Utility Methods API for PubNub Cocoa Swift SDK

This SDK has been replaced by a new PubNub Swift SDK written purely in Swift. Check it out here

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 = 1376961606timetoken = 1376961606 * 10000000timetoken = 13769616060000000

Method(s)

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

open func timeWithCompletion(_ closure: PubNub.PNTimeCompletionBlock)
ParameterTypeRequiredDescription
closurePNTimeCompletionBlockYesThe completion closure which will be called when the processing is complete, has two arguments: result - in case of successful processing (data will contain server-provided timetoken); status - in case of error while processing (errorDatacontains error information).

Basic Usage

Get PubNub Timetoken

self.client.timeWithCompletion({ (result, status) in    if status == nil {        // Handle downloaded server timetoken using: result.data.timetoken    }    else {        /**         Handle timetoken download error. Check 'category' property to find         out possible reason because of which request did fail.         Review 'errorData' property (which has PNErrorData data type) of status         object to get additional information about issue.         Request can be resent using: status.retry()
show all 18 lines

Response

Response objects which is returned by client when Time API is used:

open class PNTimeData : PNServiceData {    // Current time on PubNub network servers.    open var timetoken: NSNumber { get }}open class PNTimeResult : PNResult {    // Stores reference on time request processing information.    open var data: PNTimeData { get }}

Add Event Listener

Description

After subscription to channel(s), channel group(s) or enabling presence events handling PubNub clent start to receive real-time events. To be aware of these events listeners should be added to notification list. Client can have any number of listeners and all of them will receive updates every time when new real-time event will arrive. Target listener should conform to PNObjectEventListener protocol, in other case client will ignore it.

Method(s)

To add listener you can use the following function in the Swift SDK:

open func addListener(_ listener: PNObjectEventListener)
ParameterTypeRequiredDescription
listenerAnyYesAny object which conform to PNObjectEventListener protocol and would like to receive notifications on real-time events.

Basic Usage

Client configuration:

self.client.addListener(self)

Returns

Void

Get size of message

Description

This function provides a mechanism to calculate resulting message before it will be sent to the PubNub network.

Method(s)

To run Get size of message you can use the following method(s) in the Swift SDK:

  1. open func sizeOfMessage(_ message: Any, toChannel channel: String, withCompletion closure: PubNub.PNMessageSizeCalculationCompletionBlock)
    ParameterTypeRequiredDescription
    messageAnyYesThe messagefor which the size needs be calculated.
    channelStringYesThe channel on which the message has to be sent (it is part of request URI).
    closurePNMessageSizeCalculationCompletionBlockYesCompletion closure which will be called when the message size calculation is complete.
  2. open func sizeOfMessage(_ message: Any, toChannel channel: String, compressed compressMessage: Bool, withCompletion closure: PubNub.PNMessageSizeCalculationCompletionBlock)
    ParameterTypeRequiredDescription
    messageAnyYesThe message for which the size needs be calculated.
    channelStringYesThe channel on which the message has to be sent (it is part of request URI).
    compressMessageBoolYesShould be true if the message is compressed before sending to PubNub network.
    closurePNMessageSizeCalculationCompletionBlockYesCompletion closure which will be called when the message size calculation is complete.
  3. open func sizeOfMessage(_ message: Any, toChannel channel: String, storeInHistory shouldStore: Bool, withCompletion closure: PubNub.PNMessageSizeCalculationCompletionBlock)
    ParameterTypeRequiredDescription
    messageAnyYesThe message for which the size needs be calculated.
    channelStringYesThe channel on which the message has to be sent (it is part of request URI).
    shouldStoreBoolYesShould be true if the message is marked to be stored in history.
    closurePNMessageSizeCalculationCompletionBlockYesCompletion closure which will be called when the message size calculation is complete.
  4. open func sizeOfMessage(_ message: Any, toChannel channel: String, compressed compressMessage: Bool, storeInHistory shouldStore: Bool, withCompletion closure: PubNub.PNMessageSizeCalculationCompletionBlock)
    ParameterTypeRequiredDescription
    messageAnyYesThe message for which the size needs be calculated.
    channelStringYesThe channel on which the message has to be sent (it is part of request URI).
    compressMessageBoolYesShould be true if the message is compressed before sending to PubNub network.
    shouldStoreBoolYesShould be true if the message is marked to be stored in history.
    closurePNMessageSizeCalculationCompletionBlockYesCompletion closure which will be called when the message size calculation is complete.
  5. open func sizeOfMessage(_ message: Any, toChannel channel: String, withMetadata metadata: [String : Any]?, completion closure: PubNub.PNMessageSizeCalculationCompletionBlock)
    ParameterTypeRequiredDescription
    messageAnyYesThe message for which the size needs be calculated.
    channelStringYesThe channel on which the message has to be sent (it is part of request URI).
    metadata[String : Any]NoNSDictionary with values which should be used by PubNub service to filter messages.
    closurePNMessageSizeCalculationCompletionBlockYesCompletion closure which will be called when the message size calculation is complete.
  6. open func sizeOfMessage(_ message: Any, toChannel channel: String, compressed compressMessage: Bool, withMetadata metadata: [String : Any]?, completion closure: PubNub.PNMessageSizeCalculationCompletionBlock)
    ParameterTypeRequiredDescription
    messageAnyYesThe message for which the size needs be calculated.
    channelStringYesThe channel on which the message has to be sent (it is part of request URI).
    compressMessageBoolYesShould be true if the message is compressed before sending to PubNub network.
    metadata[String : Any]NoNSDictionary with values which should be used by PubNub service to filter messages.
    closurePNMessageSizeCalculationCompletionBlockYesCompletion closure which will be called when the message size calculation is complete.
  7. open func sizeOfMessage(_ message: Any, toChannel channel: String, storeInHistory shouldStore: Bool, withMetadata metadata: [String : Any]?, completion closure: PubNub.PNMessageSizeCalculationCompletionBlock)
    ParameterTypeRequiredDescription
    messageAnyYesThe message for which the size needs be calculated.
    channelStringYesThe channel on which the message has to be sent (it is part of request URI).
    shouldStoreBoolYesShould be true if the message is marked to be stored in history.
    metadata[String : Any]NoNSDictionary with values which should be used by PubNub service to filter messages.
    closurePNMessageSizeCalculationCompletionBlockYesCompletion closure which will be called when the message size calculation is complete.
  8. open func sizeOfMessage(_ message: Any, toChannel channel: String, compressed compressMessage: Bool, storeInHistory shouldStore: Bool, withMetadata metadata: [String : Any]?, completion closure: PubNub.PNMessageSizeCalculationCompletionBlock)
    ParameterTypeRequiredDescription
    messageAnyYesThe message for which the size needs be calculated.
    channelStringYesThe channel on which the message has to be sent (it is part of request URI).
    shouldStoreBoolYesShould be true if the message is marked to be stored in history.
    compressMessageBoolYesShould be true if the message is compressed before sending to PubNub network.
    metadata[String : Any]NoNSDictionary with values which should be used by PubNub service to filter messages.
    closurePNMessageSizeCalculationCompletionBlockYesCompletion closure which will be called when the message size calculation is complete.

Basic Usage

Get message size

self.client.sizeOfMessage(["Hello":"World"], toChannel: "announcement",                          withCompletion: { (size) in    // Process calculated target message size.})

Returns

The message size.

Other Examples

Get Size of Message with metadata

self.client.sizeOfMessage(["Hello":"World"], toChannel: "announcement",                            withMetadata:["senderID" : "bob"], completion: { (size) in    // Process calculated target message size.})

Remove Event Listener

Description

Stop listening for real-time events.

Method(s)

To remove listener you can use the following function in the Swift SDK:

open func removeListener(_ listener: PNObjectEventListener)
ParameterTypeRequiredDescription
listenerAnyYesAny object which conform to PNObjectEventListener protocol and doesn't want to receive notifications on real-time events anymore.

Basic Usage

Remove Event Listener

self.client.removeListener(self)

Returns

Void

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.

open class func encrypt(_ data: Data, withKey key: String) -> String?
ParameterTypeRequiredDescription
dataDataYesReference on Data object which should be encrypted.
keyStringYesReference on key which should be used to encrypt data basing on it.

Basic Usage

Encrypt part of message

let message = "No one should see me as plain"let messageData = message.data(using: String.Encoding.utf8, allowLossyConversion: false)let secretMessage = PNAES.encrypt(messageData!, withKey: "my_cipherkey")

Returns

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

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.

open class func decrypt(_ object: String, withKey key: String) -> Data?
ParameterTypeRequiredDescription
objectStringYesReference on previously encrypted Base64-encoded string which should be decrypted.
keyStringYesReference on key which should be used to decrypt data.

Basic Usage

Decrypt part of message

let encryptedMessage = messagePayload["secret"] as! Stringlet messageData = PNAES.decrypt(encryptedMessage, withKey: "my_cipherkey")if let decryptedData = messageData {    let decryptedMessage = NSString(data: decryptedData, encoding: String.Encoding.utf8.rawValue) as! String}

Returns

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