---
source_url: https://www.pubnub.com/docs/sdks/cocoa-swift/api-reference/misc
title: Miscellaneous API for Cocoa Swift SDK
updated_at: 2026-05-25T11:27:40.077Z
---

> Documentation Index
> For a curated overview of PubNub documentation, see: https://www.pubnub.com/docs/llms.txt
> For the full list of all documentation pages, see: https://www.pubnub.com/docs/llms-full.txt


# 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](https://www.pubnub.com/docs/sdks/swift)

## Time

This function will return a 17 digit precision Unix epoch.

:::note Algorithm constructing the timetoken
```javascript
timetoken = (Unix epoch time in seconds) * 10000000
```
Example of creating a timetoken for a specific time and date
```javascript
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:

```swift
open func timeWithCompletion(_ closure: PubNub.PNTimeCompletionBlock)
```

| Parameter | Type | Required | Default | 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). |

### Sample code

#### Get PubNub timetoken

```swift
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:

```swift
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

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:

```swift
open func addListener(_ listener: PNObjectEventListener)
```

| Parameter | Description |
| --- | --- |
| `listener` *Type: Any | Any object which conform to `PNObjectEventListener` protocol and would like to receive notifications on real-time events. |

### Sample code

Client configuration:

```swift
self.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

```swift
open func sizeOfMessage(
    _ message: Any,
    toChannel channel: String,
    withCompletion closure: PubNub.PNMessageSizeCalculationCompletionBlock
)
```

| Parameter | Description |
| --- | --- |
| `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). |
| `closure` *Type: PNMessageSizeCalculationCompletionBlock | Completion closure which will be called when the message size calculation is complete. |

#### Get size of message with compression and closure

```swift
open func sizeOfMessage(
    _ message: Any,
    toChannel channel: String,
    compressed compressMessage: Bool,
    withCompletion closure: PubNub.PNMessage
```

| Parameter | Description |
| --- | --- |
| `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

```swift
open func sizeOfMessage(
    _ message: Any,
    toChannel channel: String,
    storeInHistory shouldStore: Bool,
    withCompletion closure: PubNub.PNMessageSizeCalculationCompletionBlock
)
```

| Parameter | Description |
| --- | --- |
| `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

```swift
open func sizeOfMessage(
    _ message: Any,
    toChannel channel: String,
    compressed compressMessage: Bool,
    storeInHistory shouldStore: Bool,
    withCompletion closure: PubNub.PNMessageSizeCalculationCompletionBlock
)
```

| Parameter | Description |
| --- | --- |
| `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

```swift
open func sizeOfMessage(
    _ message: Any,
    toChannel channel: String,
    withMetadata metadata: [String: Any]?,
    completion closure: PubNub.PNMessageSizeCalculationCompletionBlock
)
```

| Parameter | Description |
| --- | --- |
| `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

```swift
open func sizeOfMessage(
    _ message: Any,
    toChannel channel: String,
    compressed compressMessage: Bool,
    withMetadata metadata: [String: Any]?,
    completion closure: PubNub.PNMessageSizeCalculationCompletionBlock
)
```

| Parameter | Description |
| --- | --- |
| `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

```swift
open func sizeOfMessage(
    _ message: Any,
    toChannel channel: String,
    storeInHistory shouldStore: Bool,
    withMetadata metadata: [String: Any]?,
    completion closure: PubNub.PNMessageSizeCalculationCompletionBlock
)
```

| Parameter | Description |
| --- | --- |
| `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

```swift
open func sizeOfMessage(
    _ message: Any,
    toChannel channel: String,
    compressed compressMessage: Bool,
    storeInHistory shouldStore: Bool,
    withMetadata metadata: [String: Any]?,
    completion closure: PubNub.PNMessageSizeCalculationCompletionBlock
)
```

| Parameter | Description |
| --- | --- |
| `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

```swift
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

```swift
self.client.sizeOfMessage(["Hello":"World"], toChannel: "announcement",
                            withMetadata:["senderID" : "bob"], completion: { (size) in

    // Process calculated target message size.
})
```

## Remove event listener

Stop listening for real-time events.

### Method(s)

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

```swift
open func removeListener(_ listener: PNObjectEventListener)
```

| Parameter | Description |
| --- | --- |
| `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

```swift
self.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.

```swift
open class func encrypt(_ data: Data, withKey key: String) -> String?
```

| Parameter | Description |
| --- | --- |
| `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

```swift
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

This function allows to `decrypt` the data.

### Method(s)

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

```swift
open class func decrypt(_ object: String, withKey key: String) -> Data?
```

| Parameter | Description |
| --- | --- |
| `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

```swift
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.