This is the official PubNub Swift SDK repository.
PubNub takes care of the infrastructure and APIs needed for the realtime communication layer of your application. Work on your app's logic and let PubNub handle sending and receiving data across the world in less than 100ms.
The PubNub Swift SDK doesn't contain any external dependencies.
You will need the publish and subscribe keys to authenticate your app. Get your keys from the Admin Portal.
You have several options to set up your project. We provide instructions here for Swift Package Manager, CocoaPods, and Carthage.
Up to Next Major Version
rule spanning from 4.0.0
< 5.0.0
, and hit the Next buttonFor more information see Apple's guide on Adding Package Dependencies to Your App
# Podfile
use_frameworks!
target 'YOUR_TARGET_NAME' do
pod 'PubNubSwift', '~> 4.0'
end
Note: Replace
YOUR_TARGET_NAME
with your target's name.
In the directory containing your Podfile
. execute the following:
pod install
Officially supported: Carthage 0.33 and up.
Add the following to Cartfile
:
github "pubnub/swift" ~> 4.0
Then in the directory containing your Cartfile
, execute the following:
carthage update
Import the module named PubNub
inside your AppDelegate:
import UIKit
import PubNub // <- Here is our PubNub module import.
Create and configure a PubNub object:
var config = PubNubConfiguration(
publishKey: "myPublishKey",
subscribeKey: "mySubscribeKey",
uuid: "myUniqueUUID"
)
let pubnub = PubNub(configuration: config)
// Create a new listener instance
let listener = SubscriptionListener()
// Add listener event callbacks
listener.didReceiveSubscription = { event in
switch event {
case let .messageReceived(message):
print("Message Received: \(message) Publisher: \(message.publisher ?? "defaultUUID")")
case let .connectionStatusChanged(status):
print("Status Received: \(status)")
case let .presenceChanged(presence):
print("Presence Received: \(presence)")
case let .subscribeError(error):
print("Subscription Error \(error)")
default:
break
}
}
// Start receiving subscription events
pubnub.add(listener)
NOTE: You can check the UUID of the publisher of a particular message by checking the
message.publisher
property in the subscription listener. You must also provide a default value forpublisher
, as theUUID
parameter is optional.
pubnub.publish(channel: "my_channel", message: "Test Message!") { result in
switch result {
case let .success(timetoken):
print("The message was successfully published at: \(timetoken)")
case let .failure(error):
print("Handle response error: \(error.localizedDescription)")
}
}
pubnub.subscribe(to: ["my_channel"])
If you need help or have a general question, contact [email protected].
The PubNub Swift SDK is released under the MIT license. See LICENSE for details.
link |
Stars: 24 |
Last commit: 4 hours ago |
August 30 2023
$PubNubAsStaticFramework
flag has been added to PubNubSwift.podspec
. With $PubNubAsStaticFramework = true
in Podfile
SDK will be integrated as a static library. Fixed the following issues reported by @Krisztiaan: #142.Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics