SwiftSwift Native SDK 2.1.2

 
These docs are for the new PubNub Swift SDK written purely in Swift. To view the docs for the Older version (the PubNub Objective-C SDK wrapper for Swift), have a look here.

The Swift Package Manager is a tool for managing the distribution of Swift code. It’s integrated with the Swift build system to automate the process of downloading, compiling, and linking dependencies. To integrate PubNub into your Xcode project using Swift Package Manager, specify it in the dependencies list of your Package.swift file:

dependencies: [
  .package(url: "https://github.com/pubnub/swift.git", from: "2.1.2")
]

To integrate PubNub into your Xcode project using Embedded Framework, do the following:

  1. In your top-level project directory add PubNub as a submodule:
    $ git submodule add https://github.com/pubnub/swift.git
  2. Open your app’s Xcode project.
  3. In Finder, locate the PubNub.xcodeproj Xcode project inside the submodule directory, and drag it onto your app's project in the Project Navigator.

    PubNub.xcodeproj

    This nests a reference to the framework project in the app project.

  4. Select your app's project in the Project Navigator (blue project icon), navigate to the target configuration window, and select the application target under the "Targets" heading in the sidebar.
  5. Select your application project in the Project Navigator, and then select your application's Target under the TARGETS panel.
  6. Select the General tab in the top middle of the window, and then click the + inside the EMBEDDED BINARIES section
  7. Select the PubNub.framework that is nested inside the top-most PubNub.xcodeproj/Products/ directory

For more information see Apple's guide on Adding Package Dependencies to Your App

CocoaPods is a dependency manager and this is by far the easiest and quickest way to get started with PubNub SDK! (If you don't have pods installed yet you can check CocoaPods installation section for installation guide).

To integrate PubNub into your Xcode project using CocoaPods, specify it in your Podfile:

pod 'PubNubSwift', '~> 2.1.2'

Carthage builds your dependencies and provides you with binary frameworks, but you retain full control over your project structure and setup. Carthage does not automatically modify your project files or your build settings. To integrate PubNub into your Xcode project using Carthage, specify it in your Cartfile:

github "pubnub/swift" ~> 2.1.2


View Supported Platforms
To add the PubNub SDK to your project with Swift Package Manager, do the following:
  1. Create a new Xcode project.

    Xcode project

  2. File -> Swift Packages -> Add Package Dependency...
  3. Search for PubNub and select the swift package owned by pubnub, and hit the Next button
  4. Use the Up to Next Major Version rule spanning from 2.0.1 < 3.0.0, and hit the Next button
  5. Import the module named PubNub inside your AppDelegate:

    import UIKit
    import PubNub // <- Here is our PubNub module import.
  6. Create a global PubNub object and pass it to your root view controller

    import UIKit
    import PubNub
    
    @UIApplicationMain
    class AppDelegate: UIResponder, UIApplicationDelegate {
    
      var pubnub: PubNub!
      var window: UIWindow?
    
      func application(_ application: UIApplication,
                       willFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
        PubNub.log.levels = [.all]
        PubNub.log.writers = [ConsoleLogWriter(), FileLogWriter()]
    
        let config = PubNubConfiguration(publishKey: "demo", subscribeKey: "demo")
        pubnub = PubNub(configuration: config)
    
        if #available(iOS 13.0, *) {
          // no-op - UI created in scene delegate
        } else if let rootVC = self.window?.rootViewController as? ViewController {
          rootVC.pubnub = pubnub
        }
    
        return true
      }
    
      // MARK: UISceneSession Lifecycle
    
      @available(iOS 13.0, *)
      func application(_ application: UIApplication,
                       configurationForConnecting connectingSceneSession: UISceneSession, 
                       options: UIScene.ConnectionOptions) -> UISceneConfiguration {
        return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
      }
    
      @available(iOS 13.0, *)
      func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
        // no-op
      }
    }
  7. If using multiple Scenes, you will use the SceneDelegate to pass your PubNub instance to your view controllers

    import UIKit
    import PubNub
    
    @available(iOS 13.0, *)
    class SceneDelegate: UIResponder, UIWindowSceneDelegate {
    
      var window: UIWindow?
      var pubnub: PubNub?
    
      func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        guard let windowScence = scene as? UIWindowScene, let pubnub = (UIApplication.shared.delegate as? AppDelegate)?.pubnub else { return }
        let mainStoryboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    
        let rootVC = mainStoryboard.instantiateInitialViewController() as? ViewController
        rootVC?.pubnub = pubnub
    
        let window = UIWindow(windowScene: windowScence)
        window.rootViewController = rootVC
        window.makeKeyAndVisible()
    
        self.window = window
      }
    }
import UIKit
import PubNub

class ViewController: UIViewController {

  var pubnub: PubNub!
  let channels = ["awesomeChannel"]
  let listener = SubscriptionListener(queue: .main)
  var token: ListenerToken?

  override func viewDidLoad() {
    super.viewDidLoad()

    listener.didReceiveMessage = { message in
      print("[Message]: \(message)")
    }
    listener.didReceiveStatus = { status in
      switch status {
      case .success(let connection):
        if connection == .connected {
          self.pubnub.publish(channel: self.channels[0], message: "Hello PubNub Swift!") { result in
            print(result.map { "Publish Response at \($0.timetoken.timetokenDate)" })
          }
        }
      case .failure(let error):
        print("Status Error: \(error.localizedDescription)")
      }
    }

    token = pubnub.add(listener)

    pubnub.subscribe(to: channels, withPresence: true)
  }
}
In addition to the Hello World sample code, we also provide some copy and paste snippets of common API functions:

Instantiate a new Pubnub instance. Only the subscribeKey is mandatory. Also include publishKey if you intend to publish from this instance.

let config = PubNubConfiguration(
  publishKey: "demo",
  subscribeKey: "demo"
)
let pubnub = PubNub(configuration: config)
// Create a new listener instance
let listener = SubscriptionListener()

// Add listener event callbacks
listener.didReceiveSubscription = { event in
  switch event {
  case messageReceived(message):
    print("Message Received: \(message)")
  case connectionStatusChanged(status):
    print("Status Received: \(status)")
  case presenceChanged(event):
    print("Presence Received: \(presence)")
  case subscribeError(PubNubError):
    print("Subscription Error \(error.localizedError)")
   default:
    break
  }
}

// Start receiving subscription events
pubnub.add(listener)

The SubscriptionListener can be removed by either calling listener.cancel(), or by letting it fall out of scope and be removed as an autoreleased object.

This is the list of event case types that can be emitted from didReceiveSubscription. Each event case also has a standalone listener that can save code space, but will be functionally equivalent to using didReceiveSubscription.

Event CaseEvent TypeStandalone ListenerEvent Description
messageReceivedMessageEventdidReceiveMessageMessages sent to subscribed channels/groups
signalReceivedMessageEventdidReceiveSignalSignals sent to subscribed channels/groups
connectionStatusChangedConnectionStatusdidReceiveStatusChanges in the connection to the PubNub system
subscriptionChangedSubscriptionChangeEventdidReceiveSubscriptionChangeNotifies when channels/groups are subscribed or unsubscribed
presenceChangedPresenceEventdidReceivePresencePresence changes on subscribed channels/groups tracking presence
userUpdatedUserEventdidReceiveUserEventA subscribed User was updated
userDeletedIdentifierEventdidReceiveUserEventA subscribed User was deleted
spaceUpdatedSpaceEvendidReceiveSpaceEventA subscribed Space was updated
spaceDeletedIdentifierEventdidReceiveSpaceEventA subscribed Space was deleted
membershipAddedMembershipEventdidReceiveMembershipEventA new membership was created
membershipUpdatedMembershipEventdidReceiveMembershipEventAn existing membership was updated
membershipDeletedMembershipIdentifiabledidReceiveMembershipEventAn existing membership was deleted
subscribeErrorPubNubErrorsubscribeErrorAny error that might have occurred during the subscription stream
messageActionAddedMessageActionEventdidReceiveMessageActionA MessageAction was added to a published message
messageActionRemovedMessageActionEventdidReceiveMessageActionA MessageAction was removed from a published message
Call pubnub.time to verify the client connectivity to the origin:
pubnub.time { result in
  switch result {
  case let .success(response):
    print("Handle downloaded server time token using: \(response.timetoken)")
  case let .failure(error):
    print("Handle response error: \(error.localizedDescription)")
  }
}
pubnub.subscribe(to: ["my_channel"])
The response of the call is handled by adding a Listener. Please see the Listeners section for more details. Listeners should be added before calling the method.
Publish a message to a channel:
pubnub.publish(channel: "my_channel", message: "Test Message!") { result in
  switch result {
  case let .success(response):
    print("Handle successful Publish response: \(response)")
  case let .failure(error):
    print("Handle response error: \(error.localizedDescription)")
  }
}
Get occupancy of who's here now on the channel by UUID:
Requires that the Presence add-on is enabled for your key. How do I enable add-on features for my keys? - see http://www.pubnub.com/knowledge-base/discussion/644/how-do-i-enable-add-on-features-for-my-keys
pubnub.hereNow(on: ["my_channel"]) { result in
  switch result {
  case let .success(response):
    print("Successful hereNow Response: \(response)")
  case let .failure(error):
    print("Failed hereNow Response: \(error.localizedDescription)")
  }
})
Subscribe to realtime Presence events, such as join, leave, and timeout, by UUID. Setting the presence attribute to a callback will subscribe to presents events on my_channel:
Requires that the Presence add-on is enabled for your key. How do I enable add-on features for my keys? - see http://www.pubnub.com/knowledge-base/discussion/644/how-do-i-enable-add-on-features-for-my-keys
pubnub.subscribe(
  to: ["my_channel"],
  withPresence: true
)
The response of the call is handled by adding a Listener. Please see the Listeners section for more details. Listeners should be added before calling the method.
Retrieve published messages from archival storage:
Requires that the Storage and Playback add-on is enabled for your key. How do I enable add-on features for my keys? - see http://www.pubnub.com/knowledge-base/discussion/644/how-do-i-enable-add-on-features-for-my-keys
pubnub.fetchMessageHistory(for: ["my_channel"]) { result in
  switch result {
  case let .success(response):
    print("Successful History Fetch Response: \(response)")
  case let .failure(error):
    print("Failed History Fetch Response: \(error.localizedDescription)")
  }
})
pubnub.unsubscribe(from: ["my_channel"])
The response of the call is handled by adding a Listener. Please see the Listeners section for more details. Listeners should be added before calling the method.