CocoaCocoaiOSSwiftCocoa Swift Misc API Reference for Realtime Apps

Go to Objects (BETA)


This function will return a 17 digit precision Unix epoch.
 
Timetoken
The timetoken is constructed using the following algorithm:
timetoken = (Unix epoch time in seconds) * 10000000
Example of creating a timetoken for a specific time & date
08/19/2013 @ 9:20pm in UTC = 1376961606
timetoken = 1376961606 * 10000000
timetoken = 13769616060000000
To fetch Time you can use the following method(s) in Swift SDK
  1. 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 time token); status - in case of error while processing (errorDatacontains error information).
self.client.timeWithCompletion({ (result, status) in

	if status == nil {

		// Handle downloaded server time token using: result.data.timetoken
	}
	else {

		/**
		 Handle time token 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 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 }
}
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.
To add listener you can use the following function in the Swift SDK:
  1. ParameterTypeRequiredDescription
    listener AnyYesAny object which conform to PNObjectEventListener protocol and would like to receive notifications on realtime events.
self.client.addListener(self)
Void
This function provides a mechanism to calculate resulting message before it will be sent to the PubNub network.
To run Get size of message you can use the following method(s) in the Swift SDK
  1. 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. 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. 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. 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. 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. 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. 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. 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.
self.client.sizeOfMessage(["Hello":"World"], toChannel: "announcement",
						  withCompletion: { (size) in
									
	// Process calculated target message size.
})
The message size
  1. self.client.sizeOfMessage(["Hello":"World"], toChannel: "announcement", 
    						  withMetadata:["senderID" : "bob"], completion: { (size) in
    									
    	// Process calculated target message size.
    })
Stop listening for realtime events.
To remove listener you can use the following function in the Swift SDK:
  1. ParameterTypeRequiredDescription
    listener
    Any
    Yes
    Any object which conform to PNObjectEventListener protocol and doesn’t want to receive notifications on realtime events anymore.
self.client.removeListener(self)
Void
This function allow to encrypt the data.
To encrypt the data you can use the following method(s) in Swift SDK.
  1. ParameterTypeRequiredDescription
    dataDataYesReference on Data object which should be encrypted.
    keyStringYesReference on key which should be used to encrypt data basing on it.
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")
Encrypted Base64-encoded string received from Foundation object. nil will be returned in case of failure.
This function allow to decrypt the data.
To decrypt the data you can use the following method(s) in Swift SDK.
  1. ParameterTypeRequiredDescription
    objectStringYesReference on previously encrypted Base64-encoded string which should be decrypted.
    keyStringYesReference on key which should be used to decrypt data.
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
}
Initial Data which has been encrypted earlier. nil will be returned in case of decryption error.