Swiftpack.co - pubnub/swift as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
pubnub/swift
PubNub native Swift SDK for iOS, MacOS, WatchOS, TvOS
.package(url: "https://github.com/pubnub/swift.git", from: "4.1.0")

PubNub Swift SDK

Platform CocoaPods Compatible Swift Package Manager compatible Carthage Compatible Build Status Codacy Coverage Grade 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 9.0+ / macOS 10.11+ / Mac Catalyst 13.0+ / tvOS 9.0+ / watchOS 2.0+
  • Xcode 11+
  • 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.

Swift Package Manager

  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 4.0.0 < 5.0.0, and hit the Next button

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

CocoaPods

# 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

Carthage

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

Configure PubNub

  1. Import the module named PubNub inside your AppDelegate:

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

    var config = PubNubConfiguration(
      publishKey: "myPublishKey",
      subscribeKey: "mySubscribeKey",
      uuid: "myUniqueUUID"
    )
    let pubnub = PubNub(configuration: config)
    

Add event listeners

// 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 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 support@pubnub.com.

License

The PubNub Swift SDK is released under the MIT license. See LICENSE for details.

GitHub

link
Stars: 13
Last commit: 1 week ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Submit a free job ad (while I'm testing this). The analytics numbers for this website are here.

Release Notes

4.1.0
4 weeks ago

September 16 2021

Added

  • Add ability to parse permissions from token provided by servers.
  • Make it possible to add component identifiers for requests.

Fixed

  • Fix bufferTooSmall and make temporary buffers for files encryption / decryption adaptive in size to provided stream buffer size.
  • Don't perform HEX-encoding of Data with FCM token which should be used as-is.

Modified

  • Use token instead of authKey (if specified) for query auth.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics