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.

Encrypt

This function allows to encrypt the data.

Availability

For more information, refer to Crypto module configuration.

Method(s)

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

encrypt(data: Data)
ParameterTypeRequiredDescription
dataDataYesThe message to encrypt

Basic Usage

Encrypt part of message

do {
let cryptoModule = CryptoModule.aesCbcCryptoModule(with: "pubnubenigma")
let messageToEncrypt = "this is message".data(using: .utf8) ?? Data()
let encryptedMessage = try cryptoModule.encrypt(data:messageToEncrypt).get()
} catch {
// ...
}

Returns

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

Decrypt

This function allows to decrypt the data.

Availability

For more information, refer to Crypto module configuration.

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

do {
let cryptoModule = CryptoModule.aesCbcCryptoModule(with: "pubnubenigma")
let messageToEncrypt = "this is message".data(using: .utf8) ?? Data()
let encryptedMessage = try cryptoModule.encrypt(data:messageToEncrypt).get()
let decryptedString = String(
data: try cryptoModule.decrypt(data:encryptedMessage).get(),
encoding: .utf8
)
} catch {
// ...
}

Returns

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

Disconnect

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

Methods

To disconnect the data you can use the following methods in Swift SDK.

disconnect( )

Basic Usage

pubnub.disconnect()

Reconnect

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

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