On this page

Miscellaneous API for Cocoa Swift SDK

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

Time​

This function will return a 17 digit precision Unix epoch.

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

Example of creating a timetoken for a specific time and date

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

Method(s)​

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

1open func timeWithCompletion(_ closure: PubNub.PNTimeCompletionBlock)
* required
ParameterDescription
closure *
Type: PNTimeCompletionBlock
The 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).

Sample code​

Get PubNub timetoken​

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

Response​

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

1open class PNTimeData : PNServiceData {
2
3 // Current time on PubNub network servers.
4 open var timetoken: NSNumber { get }
5}
6
7open class PNTimeResult : PNResult {
8
9 // Stores reference on time request processing information.
10 open var data: PNTimeData { get }
11}

Add event listener​

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:

1open func addListener(_ listener: PNObjectEventListener)
* required
ParameterDescription
listener *
Type: Any
Any object which conform to PNObjectEventListener protocol and would like to receive notifications on real-time events.

Sample code​

Client configuration:

1self.client.addListener(self)

Returns​

Void

Get size of message​

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:

Get size of message with closure​

1open func sizeOfMessage(
2 _ message: Any,
3 toChannel channel: String,
4 withCompletion closure: PubNub.PNMessageSizeCalculationCompletionBlock
5)
* required
ParameterDescription
message *
Type: Any
The messagefor which the size needs be calculated.
channel *
Type: String
The channel on which the message has to be sent (it is part of request URI).
closure *
Type: PNMessageSizeCalculationCompletionBlock
Completion closure which will be called when the message size calculation is complete.

Get size of message with compression and closure​

1open func sizeOfMessage(
2 _ message: Any,
3 toChannel channel: String,
4 compressed compressMessage: Bool,
5 withCompletion closure: PubNub.PNMessage
* required
ParameterDescription
message *
Type: Any
The message for which the size needs be calculated.
channel *
Type: String
The channel on which the message has to be sent (it is part of request URI).
compressMessage *
Type: Bool
Should be true if the message is compressed before sending to PubNub network.
closure *
Type: PNMessageSizeCalculationCompletionBlock
Completion closure which will be called when the message size calculation is complete.

Get size of message with storage and closure​

1open func sizeOfMessage(
2 _ message: Any,
3 toChannel channel: String,
4 storeInHistory shouldStore: Bool,
5 withCompletion closure: PubNub.PNMessageSizeCalculationCompletionBlock
6)
* required
ParameterDescription
message *
Type: Any
The message for which the size needs be calculated.
channel *
Type: String
The channel on which the message has to be sent (it is part of request URI).
shouldStore *
Type: Bool
Should be true if the message is marked to be stored in Message Persistence.
closure *
Type: PNMessageSizeCalculationCompletionBlock
Completion closure which will be called when the message size calculation is complete.

Get size of message with compression, storage, and closure​

1open func sizeOfMessage(
2 _ message: Any,
3 toChannel channel: String,
4 compressed compressMessage: Bool,
5 storeInHistory shouldStore: Bool,
6 withCompletion closure: PubNub.PNMessageSizeCalculationCompletionBlock
7)
* required
ParameterDescription
message *
Type: Any
The message for which the size needs be calculated.
channel *
Type: String
The channel on which the message has to be sent (it is part of request URI).
compressMessage *
Type: Bool
Should be true if the message is compressed before sending to PubNub network.
shouldStore *
Type: Bool
Should be true if the message is marked to be stored in Message Persistence.
closure *
Type: PNMessageSizeCalculationCompletionBlock
Completion closure which will be called when the message size calculation is complete.

Get size of message with metadata and closure​

1open func sizeOfMessage(
2 _ message: Any,
3 toChannel channel: String,
4 withMetadata metadata: [String: Any]?,
5 completion closure: PubNub.PNMessageSizeCalculationCompletionBlock
6)
* required
ParameterDescription
message *
Type: Any
The message for which the size needs be calculated.
channel *
Type: String
The channel on which the message has to be sent (it is part of request URI).
metadata
Type: [String : Any]
NSDictionary with values which should be used by PubNub service to filter messages.
closure *
Type: PNMessageSizeCalculationCompletionBlock
Completion closure which will be called when the message size calculation is complete.

Get size of message with compression, metadata, and closure​

1open func sizeOfMessage(
2 _ message: Any,
3 toChannel channel: String,
4 compressed compressMessage: Bool,
5 withMetadata metadata: [String: Any]?,
6 completion closure: PubNub.PNMessageSizeCalculationCompletionBlock
7)
* required
ParameterDescription
message *
Type: Any
The message for which the size needs be calculated.
channel *
Type: String
The channel on which the message has to be sent (it is part of request URI).
compressMessage *
Type: Bool
Should be true if the message is compressed before sending to PubNub network.
metadata
Type: [String : Any]
NSDictionary with values which should be used by PubNub service to filter messages.
closure *
Type: PNMessageSizeCalculationCompletionBlock
Completion closure which will be called when the message size calculation is complete.

Get size of message with storage, metadata, and closure​

1open func sizeOfMessage(
2 _ message: Any,
3 toChannel channel: String,
4 storeInHistory shouldStore: Bool,
5 withMetadata metadata: [String: Any]?,
6 completion closure: PubNub.PNMessageSizeCalculationCompletionBlock
7)
* required
ParameterDescription
message *
Type: Any
The message for which the size needs be calculated.
channel *
Type: String
The channel on which the message has to be sent (it is part of request URI).
shouldStore *
Type: Bool
Should be true if the message is marked to be stored in Message Persistence.
metadata
Type: [String : Any]
NSDictionary with values which should be used by PubNub service to filter messages.
closure *
Type: PNMessageSizeCalculationCompletionBlock
Completion closure which will be called when the message size calculation is complete.

Get size of message with storage, compression, metadata, and closure​

1open func sizeOfMessage(
2 _ message: Any,
3 toChannel channel: String,
4 compressed compressMessage: Bool,
5 storeInHistory shouldStore: Bool,
6 withMetadata metadata: [String: Any]?,
7 completion closure: PubNub.PNMessageSizeCalculationCompletionBlock
8)
* required
ParameterDescription
message *
Type: Any
The message for which the size needs be calculated.
channel *
Type: String
The channel on which the message has to be sent (it is part of request URI).
shouldStore *
Type: Bool
Should be true if the message is marked to be stored in Message Persistence.
compressMessage *
Type: Bool
Should be true if the message is compressed before sending to PubNub network.
metadata
Type: [String : Any]
NSDictionary with values which should be used by PubNub service to filter messages.
closure *
Type: PNMessageSizeCalculationCompletionBlock
Completion closure which will be called when the message size calculation is complete.

Sample code​

Get message size​

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

Returns​

The message size.

Other examples​

Get size of message with metadata​

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

Remove event listener​

Stop listening for real-time events.

Method(s)​

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

1open func removeListener(_ listener: PNObjectEventListener)
* required
ParameterDescription
listener *
Type: Any
Any object which conform to PNObjectEventListener protocol and doesn't want to receive notifications on real-time events anymore.

Sample code​

Remove event listener​

1self.client.removeListener(self)

Returns​

Void

Encrypt​

This function allows to encrypt the data.

Method(s)​

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

1open class func encrypt(_ data: Data, withKey key: String) -> String?
* required
ParameterDescription
data *
Type: Data
Reference on Data object which should be encrypted.
key *
Type: String
Reference on key which should be used to encrypt data basing on it.

Sample code​

Encrypt part of message​

1let message = "No one should see me as plain"
2let messageData = message.data(using: String.Encoding.utf8, allowLossyConversion: false)
3let 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​

This function allows to decrypt the data.

Method(s)​

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

1open class func decrypt(_ object: String, withKey key: String) -> Data?
* required
ParameterDescription
object *
Type: String
Reference on previously encrypted Base64-encoded string which should be decrypted.
key *
Type: String
Reference on key which should be used to decrypt data.

Sample code​

Decrypt part of message​

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

Returns​

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