Skip to content
/ swift Public

PubNub native Swift SDK for iOS, MacOS, WatchOS, TvOS

License

Notifications You must be signed in to change notification settings

pubnub/swift

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PubNub Swift SDK

Platform CocoaPods Compatible Swift Package Manager compatible Carthage Compatible Build Status Codacy Badge

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.

Requirements

  • iOS 12.0+ / macOS 10.15+ / tvOS 12.0+ / watchOS 4.0+ / visionOS 1.0+
  • Xcode 15+
  • Swift 5+

The PubNub Swift SDK doesn't contain any external dependencies.

Get keys

You will need the publish and subscribe keys to authenticate your app. Get your keys from the Admin Portal.

Set up your project

You have several options to set up your project. We provide instructions here for Swift Package Manager, CocoaPods, and Carthage.

  1. Create or open your project inside of Xcode
  2. Navigate to 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 8.0.0 < 9.0.0, and hit the Next button

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

# Podfile
use_frameworks!

target 'YOUR_TARGET_NAME' do
  pod 'PubNubSwift', '~> 8.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.39.1 and up.

Add the following to Cartfile:

github "pubnub/swift" ~> 8.0

Then in the directory containing your Cartfile, execute the following:

carthage update --use-xcframeworks

Configure PubNub

  1. Import the module named PubNub inside your AppDelegate:

    import UIKit
    import PubNubSDK // <- Here is our PubNub module import.
  2. Create and configure a PubNub object:

    let config = PubNubConfiguration(
      publishKey: "myPublishKey",
      subscribeKey: "mySubscribeKey",
      userId: "myUniqueUserId"
    )
    let pubnub = PubNub(configuration: config)

Add event listeners

// Create a new listener instance
let subscription = pubnub.channel("channelName").subscription()

// Add listener event callbacks
subscription.onEvent = { event in
  switch event {
  case .messageReceived(let message):
    print("Message Received: \(message) Publisher: \(message.publisher ?? "defaultUserID")")
  case .presenceChanged(let presenceChange):
    print("Presence Received: \(presenceChange)")
  case .appContextChanged(let appContextEvent):
    print("App Context Event: \(appContextEvent)")
  case .messageActionChanged(let messageActionEvent):
    print("Message Action Event: \(messageActionEvent)")
  case .fileChanged(let fileEvent):
    print("File Event: \(fileEvent)")
  case .signalReceived(let message):
    print("Signal Received: \(message) Publisher: \(message.publisher ?? "defaultUserID")")
  }
}

// Start receiving subscription events
subscription.subscribe()

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 for publisher, as the UUID parameter is optional.

Publish and subscribe

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"])

Documentation

Support

If you need help or have a general question, contact [email protected].

License

The PubNub Swift SDK is released under the PubNub Software Development Kit License.

See LICENSE for details.

About

PubNub native Swift SDK for iOS, MacOS, WatchOS, TvOS

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages