Cocoa Swift Misc API Reference for Realtime Apps
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.
The timetoken is constructed using the following algorithm:
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:
open func timeWithCompletion(_ closure: PubNub.PNTimeCompletionBlock)
Parameter Type Required Description closure
PNTimeCompletionBlock Yes 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 (errorData
contains error information).
Basic Usage
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()
*/
}
})
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 realtime 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 realtime 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)
Parameter | Type | Required | Description |
---|---|---|---|
listener | Any | Yes | Any object which conform to PNObjectEventListener protocol and would like to receive notifications on realtime 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:
open func sizeOfMessage(_ message: Any, toChannel channel: String, withCompletion closure: PubNub.PNMessageSizeCalculationCompletionBlock)
Parameter Type Required Description message
Any Yes The message
for which the size needs be calculated.channel
String Yes The channel
on which themessage
has to be sent (it is part of request URI).closure
PNMessageSizeCalculationCompletionBlock Yes Completion closure which will be called when the message size calculation is complete. open func sizeOfMessage(_ message: Any, toChannel channel: String, compressed compressMessage: Bool, withCompletion closure: PubNub.PNMessageSizeCalculationCompletionBlock)
Parameter Type Required Description message
Any Yes The message for which the size needs be calculated. channel
String Yes The channel
on which themessage
has to be sent (it is part of request URI).compressMessage
Bool Yes Should be true
if the message is compressed before sending to PubNub network.closure
PNMessageSizeCalculationCompletionBlock Yes Completion closure which will be called when the message size calculation is complete. open func sizeOfMessage(_ message: Any, toChannel channel: String, storeInHistory shouldStore: Bool, withCompletion closure: PubNub.PNMessageSizeCalculationCompletionBlock)
Parameter Type Required Description message
Any Yes The message for which the size needs be calculated. channel
String Yes The channel
on which themessage
has to be sent (it is part of request URI).shouldStore
Bool Yes Should be true
if themessage
is marked to be stored in history.closure
PNMessageSizeCalculationCompletionBlock Yes Completion closure which will be called when the message size calculation is complete. open func sizeOfMessage(_ message: Any, toChannel channel: String, compressed compressMessage: Bool, storeInHistory shouldStore: Bool, withCompletion closure: PubNub.PNMessageSizeCalculationCompletionBlock)
Parameter Type Required Description message
Any Yes The message for which the size needs be calculated. channel
String Yes The channel
on which themessage
has to be sent (it is part of request URI).compressMessage
Bool Yes Should be true
if the message is compressed before sending to PubNub network.shouldStore
Bool Yes Should be true
if themessage
is marked to be stored in history.closure
PNMessageSizeCalculationCompletionBlock Yes Completion closure which will be called when the message size calculation is complete. open func sizeOfMessage(_ message: Any, toChannel channel: String, withMetadata metadata: [String : Any]?, completion closure: PubNub.PNMessageSizeCalculationCompletionBlock)
Parameter Type Required Description message
Any Yes The message
for which the size needs be calculated.channel
String Yes The channel
on which themessage
has to be sent (it is part of request URI).metadata
[String : Any] No NSDictionary
with values which should be used byPubNub
service to filter messages.closure
PNMessageSizeCalculationCompletionBlock Yes Completion closure
which will be called when themessage
size calculation is complete.open func sizeOfMessage(_ message: Any, toChannel channel: String, compressed compressMessage: Bool, withMetadata metadata: [String : Any]?, completion closure: PubNub.PNMessageSizeCalculationCompletionBlock)
Parameter Type Required Description message
Any Yes The message
for which the size needs be calculated.channel
String Yes The channel
on which themessage
has to be sent (it is part of request URI).compressMessage
Bool Yes Should be true
if themessage
is compressed before sending toPubNub
network.metadata
[String : Any] No NSDictionary
with values which should be used byPubNub
service to filter messages.closure
PNMessageSizeCalculationCompletionBlock Yes Completion closure
which will be called when themessage
size calculation is complete.open func sizeOfMessage(_ message: Any, toChannel channel: String, storeInHistory shouldStore: Bool, withMetadata metadata: [String : Any]?, completion closure: PubNub.PNMessageSizeCalculationCompletionBlock)
Parameter Type Required Description message
Any Yes The message
for which the size needs be calculated.channel
String Yes The channel
on which themessage
has to be sent (it is part of request URI).shouldStore
Bool Yes Should be true
if themessage
is marked to be stored in history.metadata
[String : Any] No NSDictionary
with values which should be used byPubNub
service to filter messages.closure
PNMessageSizeCalculationCompletionBlock Yes Completion closure
which will be called when themessage
size calculation is complete.open func sizeOfMessage(_ message: Any, toChannel channel: String, compressed compressMessage: Bool, storeInHistory shouldStore: Bool, withMetadata metadata: [String : Any]?, completion closure: PubNub.PNMessageSizeCalculationCompletionBlock)
Parameter Type Required Description message
Any Yes The message
for which the size needs be calculated.channel
String Yes The channel
on which themessage
has to be sent (it is part of request URI).shouldStore
Bool Yes Should be true
if themessage
is marked to be stored in history.compressMessage
Bool Yes Should be true
if themessage
is compressed before sending to PubNub network.metadata
[String : Any] No NSDictionary
with values which should be used byPubNub
service to filter messages.closure
PNMessageSizeCalculationCompletionBlock Yes Completion closure
which will be called when themessage
size calculation is complete.
Basic Usage
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 realtime events.
Method(s)
To remove listener you can use the following function in the Swift SDK:
open func removeListener(_ listener: PNObjectEventListener)
Parameter Type Required Description listener
Any Yes Any object which conform to PNObjectEventListener
protocol and doesn't want to receive notifications on realtime events anymore.
Basic Usage
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?
Parameter Type Required Description data
Data Yes Reference on Data
object which should be encrypted.key
String Yes Reference on key
which should be used toencrypt
data basing on it.
Basic Usage
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?
Parameter Type Required Description object
String Yes Reference on previously encrypted Base64-encoded
string which should be decrypted.key
String Yes Reference on key
which should be used todecrypt
data.
Basic Usage
let encryptedMessage = messagePayload["secret"] as! String
let 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.