Utility Methods API for PubNub Swift Native SDK

Migration guide

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.

The methods on this page are utility methods that don't fit into other categories.

Close PubNub

Description

Stops the subscriptions in progress.

Basic Usage

Close pubnub

pubnub.subscription.disconnect()

Decrypt

Description

This function allows to decrypt the data.

Method(s)

To decrypt the data you can use the following method(s) in Swift SDK.

decrypt(data: Data)
ParameterTypeRequiredDescription
dataDataYesThe data to decrypt.

Basic Usage

Decrypt part of message

let decryptedString = pubnub.decrypt("data to decrypt")

Returns

Initial Data which has been encrypted earlier. nil will be returned in case of decryption error.

Encrypt

Description

This function allows to encrypt the data.

Method(s)

To encrypt the data you can use the following method(s) in Swift SDK.

encrypt(message: String)
ParameterTypeRequiredDescription
messageStringYesThe message to encrypt

Basic Usage

Encrypt part of message

let encryptedMessage = pubnub.encrypt("message to encrypt")

Returns

Encrypted Base64-encoded string received from Foundation object. nil will be returned in case of failure.

Reconnect

Description

Call the reconnect method to force the SDK to try and reach out PubNub.

Method(s)

To reconnect the data you can use the following method(s) in Swift SDK.

reconnect( at timetoken: Timetoken, setting incomingState: ChannelPresenceState? = nil )
ParameterTypeRequiredDefaultDescription
atTimetokenYesThe timetoken to reconnect the subscribe at
settingHashMapOptionalnilThe Hashmap to reconnect the subscribe with

Basic Usage

pubnub.subscription.reconnect()

Time

Description

This function will return a 17 digit precision Unix epoch.

Algorithm constructing the timetoken
timetoken = (Unix epoch time in seconds) * 10000000

Example of creating a timetoken for a specific time and date:

08/19/2013 @ 9:20pm in UTC = 1376961606
timetoken = 1376961606 * 10000000
timetoken = 13769616060000000

Method(s)

To fetch Time you can use the following method(s) in Swift SDK:

time(  custom requestConfig: RequestConfiguration = RequestConfiguration(),  completion: ((Result<Timetoken, Error>) -> Void)?)
ParameterTypeRequiredDefaultDescription
customRequestConfigurationOptionalRequestConfiguration()An object that allows for per-request customization of PubNub Configuration or Network Session
completion((Result<Timetoken, Error>) -> Void)?OptionalnilThe async Result of the method call

Completion Handler Result

Success

The current Timetoken.

Failure

An Error describing the failure.

Basic Usage

Get PubNub Timetoken

pubnub.time { result in
switch result {
case let .success(timetoken):
print("Handle downloaded server timetoken: \(timetoken)")
case let .failure(error):
print("Handle response error: \(error.localizedDescription)")
}
}
Last updated on