SwiftSwift Native Configuration API Reference for Realtime Apps

 
The PubNub Swift 3.0 SDK contains many significant changes from the 2.x SDK, including breaking changes. Please refer to the PubNub Swift 3.0 Migration Guide for more details.

A configuration object that defines behavior and policies for a PubNub instance

Creates a configuration using the specified PubNub Publish and Subscribe Keys:

To Initialize Pubnub you can use the following method(s) in the Swift SDK:
  1. ParameterTypeRequiredDefaultsDescription
    publishKeyString?OptionalnilSpecifies the PubNub Publish Key to be used when publishing messages to a channel.
    subscribeKeyString?YesnilSpecifies the PubNub Subscribe Key to be used when subscribing to a channel.
    cipherKeyCrypto?OptionalnilIf set, all communication will be encrypted with this key.
    authKeyStringOptionalnilIf Access Manager (PAM) is enabled, client will use authKey on all requests.
    uuidStringOptionalSDK generated random UUIDUUID to use. You should set a unique UUID to identify the user or the device that connects to PubNub.
    Allowing the SDK to generate a random UUID can result in significant billing impacts, particularly on an MAU pricing plan.
    useSecureConnectionsBoolOptionaltrueIf true, requests will be made over HTTPS; otherwise they will use HTTP. You will still need to disable ATS for the system to allow insecure network traffic. See Apple’s documentation for further details.
    originStringOptional"ps.pndsn.com"Domain name used for requests.
    useInstanceIdBoolOptionalfalseWhether a PubNub object instanceId should be included on outgoing requests.
    useRequestIdBoolOptionalfalseWhether a request identifier should be included on outgoing requests.
    automaticRetryAutomaticRetry?OptionalnilReconnection policy which will be used if/when a request fails.
    urlSessionConfigurationURLSessionConfigurationYesURLSessionConfiguration.pubnubURLSessionConfiguration used for URLSession network events.
    durationUntilTimeoutIntOptional300How long (in seconds) the server will consider the client alive for presence.
    Minimum value is 20.
    heartbeatIntervalUIntOptional0How often (in seconds) the client will announce itself to server.
    Minimum value is 0.
    suppressLeaveEventsBoolOptionalfalseWhether to send out the leave requests.
    requestMessageCountThresholdUIntOptional100The number of messages into the payload before emitting RequestMessageCountExceeded.
    filterExpressionString?OptionalnilPSV2 feature to subscribe with a custom filter expression.
 Always set the UUID to uniquely identify the user or device that connects to PubNub. This UUID should be persisted, and should remain unchanged for the lifetime of the user or the device. Not setting the UUID can significantly impact your billing if your account uses the Monthly Active Users (MAUs) based pricing model, and can also lead to unexpected behavior if you have Presence enabled.
let config = PubNubConfiguration(
  publishKey: "demo",
  subscribeKey: "demo",
  uuid: "myUniqueUUID"
)

let pubnub = PubNub(configuration: config)
  1. In the case where a client will only read messages and never publish to a channel, you can simply omit the publishKey when initializing the client:
     Always set the UUID to uniquely identify the user or device that connects to PubNub. This UUID should be persisted, and should remain unchanged for the lifetime of the user or the device. Not setting the UUID can significantly impact your billing if your account uses the Monthly Active Users (MAUs) based pricing model, and can also lead to unexpected behavior if you have Presence enabled.
    let config = PubNubConfiguration(
      subscribeKey: "demo"
    )
    let pubnub = PubNub(configuration: config)

All PubNubConfiguration properties are mutable, and can be changed after the object has been initialized. However, once the configuration is set on a PubNub instance those configurations are locked and can't be changed. Any changes would require a creating new PubNub instance.

var config = pubnub.configuration
config.uuid = "my_new_UUID"

let pubnub = PubNub(configuration: config)
var config = pubnub.configuration
config.authKey = "my_new_authkey"

let pubnub = PubNub(configuration: config)

You can override the subscribe filter expression without creating a new PubNub instance in one of two ways.

 

These are functionally equivalent, and will take effect on the next subscription request and persist until changed.

  1. In combination with a subscribe change:

    pubnub.subscribe(to: ["new_subscription"], filterOverride: "(senderID=='my_new_UUID')")
  2. Without needing to make a subscription change:

    pubnub.subscribeFilterExpression = "(senderID=='my_new_UUID')"