Configuration API for PubNub Objective-C SDK

Objective-C complete API reference for building real-time applications on PubNub, including basic usage and sample code.

Configuration

PNConfiguration instance is storage for user-provided information which describe further PubNub client behavior. Configuration instance contain additional set of properties which allow to perform precise PubNub client configuration.

Privacy

MAU billing tracks users (Device and MAU) for analytics and billing. PubNub does not track customers using Transactions with random UUIDs/UserIDs.

Method(s)

To create configuration instance you can use the following function in the Objective-C SDK:

+ (instancetype)configurationWithPublishKey:(NSString *)publishKey 
subscribeKey:(NSString *)subscribeKey;
userID:(NSString *)userID
ParameterTypeRequiredDescription
publishKeyNSStringYesKey which allow client to use data push API.
subscribeKeyNSStringYesKey which allow client to subscribe on live feeds pushed from PubNub service.
userIDNSStringYesuserID to use. You should set a unique identifier for the user or the device that connects to PubNub. It's a UTF-8 encoded string of up to 92 alphanumeric characters. If you don't set the userID, you won't be able to connect to PubNub.
heartbeatNotificationOptionsPNHeartbeatNotificationOptionsNoThese are bitmask options, they can be combined. When client instances are notified about heartbeat operations, this happens through the PNObjectEventListener callback for statuses.
PNHeartbeatNotifySuccess: explicitly tells client to notify on successful heartbeat operations.
PNHeartbeatNotifyFailure: explicitly tells client to notify on failed heartbeat operations (default is only this option)
PNHeartbeatNotifyAll: This is a combination of PNHeartbeatNotifySuccess and PNHeartbeatNotifyFailure
PNHeartbeatNotifyNone: This means the client will not provide any callback notifications for heartbeat operations.
stripMobilePayloadBOOLNoStores whether client should strip out received messages (real-time and history) from data which has been appended by client (like mobile payload for mobile push notifications).
subscribeMaximumIdleTimeNSTimeIntervalNoMaximum number of seconds which client should wait for events from live feed.
Default 310.
nonSubscribeRequestTimeoutNSTimeIntervalNoNumber of seconds which is used by client during non-subscription operations to check whether response potentially failed with timeout or not.
Default 10.
presenceHeartbeatValueNSIntegerNoNumber of seconds which is used by server to track whether client still subscribed on remote data objects live feed or not.
presenceHeartbeatIntervalNSIntegerNoNumber of seconds which is used by client to issue heartbeat requests to PubNub service.
keepTimeTokenOnListChangeBOOLNoWhether client should keep previous timetoken when subscribe on new set of remote data objects live feeds.
Default YES.
catchUpOnSubscriptionRestoreBOOLNoWhether client should try to catch up for events which occurred on previously subscribed remote data objects feed while client was off-line.
Default YES.
applicationExtensionSharedGroupIdentifierNSStringNoReference on group identifier which is used to share request cache between application extension and it's containing application.
This property should be set to valid registered group only if PubNub client is used inside of application's extension (iOS 8.0+, macOS 10.10+).
requestMessageCountThresholdNSUIntegerNoNumber of maximum expected messages from PubNub service in single response.
maximumMessagesCacheSizeNSUIntegerNoMessages de-duplication cache size
Default 100.
completeRequestsBeforeSuspensionBOOLNoWhether client should try complete all API call which is done before application will be completely suspended.
Default YES.
suppressLeaveEventsBOOLNoIf YES, the client shouldn't send presence leave events during the unsubscribe process.
originNSStringNoIf a custom domain is required, SDK accepts it here.
requestRetryPNRequestRetryConfigurationNoCustom reconnection configuration parameters. For more information, refer to requestRetry.
cryptoModule[PNCryptoModule AESCBCCryptoModuleWithCipherKey: NSString randomInitializationVector: BOOL];
[PNCryptoModule legacyCryptoModuleWithCipherKey: NSString randomInitializationVector: BOOL];
NoThe cryptography module used for encryption and decryption of messages and files. Takes the cipherKey and useRandomInitializationVector parameters as arguments.

For more information, refer to the cryptoModule section.
cipherKeyNSStringNoThis way of setting this parameter is deprecated, pass it to cryptoModule instead.

Key which is used to encrypt messages pushed to PubNub service and decrypt messages received from live feeds on which client subscribed at this moment.
useRandomInitializationVectorBOOLNoThis way of setting this parameter is deprecated, pass it to cryptoModule instead.

When YES the initialization vector (IV) is random for all requests (not just for file upload). When NO the IV is hard-coded for all requests except for file upload. By default, using the random initialization vector is enabled.
Disabling random initialization vector

Disable random initialization vector (IV) only for backward compatibility (<4.16.0) with existing applications. Never disable random IV on new applications.

requestRetry

The PNRequestRetryConfiguration is the retry configuration object and has the following methods. For more general information about the policies and parameters, refer to Reconnection Policy.

Create a default linear retry policy
+ (instancetype)configurationWithLinearDelay;
Example
configuration.requestRetry = [PNRequestRetryConfiguration configurationWithLinearDelay];
Create a linear retry policy with excluded endpoints
+ (instancetype)configurationWithLinearDelayExcludingEndpoints:(PNEndpoint)endpoints, ...;
ParameterTypeDescription
endpointsPNEndpointThe endpoints to exclude. For a list of endpoints, inspect NS_ENUM(NSUInteger, PNEndpoint) in the PNStructures file.
Example
configuration.requestRetry = [PNRequestRetryConfiguration configurationWithLinearDelayExcludingEndpoints:PNMessageSendEndpoint, 0];
Create a linear retry policy with excluded endpoints and custom parameters
+ (instancetype)configurationWithLinearDelay:(NSTimeInterval)delay
maximumRetry:(NSUInteger)maximumRetry
excludedEndpoints:(PNEndpoint)endpoints, ...;
ParameterTypeDescription
delayNSTimeIntervalDelay in seconds between failed requests.
maximumRetryNSUIntegerHow many times to retry before throwing an error.
excludedEndpointsPNEndpointThe endpoints to exclude. For a list of endpoints, inspect NS_ENUM(NSUInteger, PNEndpoint) in the PNStructures file.
Example
/// example
configuration.requestRetry = [PNRequestRetryConfiguration configurationWithLinearDelay:3.f
maximumRetry:3
excludedEndpoints:PNMessageSendEndpoint, PNMessageStorageEndpoint, 0];
Create a default exponential retry policy
+ (instancetype)configurationWithExponentialDelay;
Example
configuration.requestRetry = [PNRequestRetryConfiguration configurationWithExponentialDelay];
Create an exponential retry policy with excluded endpoints
+ (instancetype)configurationWithExponentialDelayExcludingEndpoints:(PNEndpoint)endpoints, ...;
ParameterTypeDescription
endpointsPNEndpointThe endpoints to exclude. For a list of endpoints, inspect NS_ENUM(NSUInteger, PNEndpoint) in the PNStructures file.
Example
configuration.requestRetry = [PNRequestRetryConfiguration configurationWithExponentialDelayExcludingEndpoints:PNMessageSendEndpoint, 0];
Create an exponential retry policy with excluded endpoints and custom parameters
+ (instancetype)configurationWithExponentialDelay:(NSTimeInterval)minimumDelay
maximumDelay:(NSTimeInterval)maximumDelay
maximumRetry:(NSUInteger)maximumRetry
excludedEndpoints:(PNEndpoint)endpoints, ...;
ParameterTypeDescription
minimumDelayNSTimeIntervalBase delay in seconds used to calculate the next delay.
maximumDelayNSTimeIntervalMaximum allowed computed delay in seconds between retry attempts.
maximumRetryNSUIntegerHow many times to retry before throwing an error.
excludedEndpointsPNEndpointThe endpoints to exclude. For a list of endpoints, inspect NS_ENUM(NSUInteger, PNEndpoint) in the PNStructures file.
Example
configuration.requestRetry = [PNRequestRetryConfiguration configurationWithExponentialDelay:3.f
maximumDelay:120.f
maximumRetry:3
excludedEndpoints:PNMessageSendEndpoint, PNMessageStorageEndpoint, 0];

cryptoModule

cryptoModule provides encrypt/decrypt functionality for messages and files. From the 5.1.3 release on, you can configure how the actual encryption/decryption algorithms work.

Each PubNub SDK is bundled with two ways of encryption: the legacy encryption with 128-bit cipher key entropy and the recommended 256-bit AES-CBC encryption. For more general information on how encryption works, refer to the Message Encryption and File Encryption sections.

If you do not explicitly set the cryptoModule in your app and have the cipherKey and useRandomInitializationVector params set in PubNub config, the client defaults to using the legacy encryption.

Legacy encryption with 128-bit cipher key entropy

You don't have to change your encryption configuration if you want to keep using the legacy encryption. If you want to use the recommended 256-bit AES-CBC encryption, you must explicitly set that in PubNub config.

cryptoModule configuration

To configure the cryptoModule to encrypt all messages/files, you can use the following methods in the Objective-C SDK:

// encrypts using 256-bit AES-CBC cipher (recommended)
// decrypts data encrypted with the legacy and the 256-bit AES-CBC ciphers
config.cryptoModule = [PNCryptoModule AESCBCCryptoModuleWithCipherKey:@"enigma"
randomInitializationVector:YES];

// encrypts with 128-bit cipher key entropy (legacy)
// decrypts data encrypted with the legacy and the 256-bit AES-CBC ciphers
config.cryptoModule = [PNCryptoModule legacyCryptoModuleWithCipherKey:@"enigma"
randomInitializationVector:YES];

Your client can decrypt content encrypted using either of the modules. This way, you can interact with historical messages or messages sent from older clients while encoding new messages using the more secure 256-bit AES-CBC cipher.

Older SDK versions

Apps built using the SDK versions lower than 5.1.3 will not be able to decrypt data encrypted using the 256-bit AES-CBC cipher. Make sure to update your clients or encrypt data using the legacy algorithm.

Basic Usage

Required UserId

Always set the userID to uniquely identify the user or device that connects to PubNub. This userID should be persisted, and should remain unchanged for the lifetime of the user or the device. If you don't set the userID, you won't be able to connect to PubNub.

PNConfiguration *config = [PNConfiguration configurationWithPublishKey:@"demo"
subscribeKey:@"demo"
userID:@"myUserID"];

self.client = [PubNub clientWithConfiguration:config];
[self.client addListener:self];

Returns

Configured and ready to use client configuration instance.

Other Examples

Configure heartbeat notifications

PNConfiguration
PNConfiguration *config = [PNConfiguration configurationWithPublishKey:@"<pub key>" subscribeKey:@"<sub key>"];
/**
This is where you need to adjust the PNConfiguration object for the types of heartbeat notifications you want.
This is a bitmask of options located at https://github.com/pubnub/objective-c/blob/1f1c7a41a3bd8c32b644a6ad98fe179d45397c2b/PubNub/Misc/PNStructures.h#L24
*/
config.heartbeatNotificationOptions = PNHeartbeatNotifyAll;

self.client = [PubNub clientWithConfiguration:config];
[self.client addListener:self];
Listener
- (void)client:(PubNub *)client didReceiveStatus:(PNStatus *)status {

if (status.operation == PNHeartbeatOperation) {

/**
Heartbeat operations can in fact have errors, so it is important to check first for an error.
For more information on how to configure heartbeat notifications through the status
PNObjectEventListener callback, consult http://www.pubnub.com/docs/sdks/objective-c/api-reference/configuration#configuration_basic_usage
*/

if (!status.isError) { /* Heartbeat operation was successful. */ }
else { /* There was an error with the heartbeat operation, handle here. */ }
}
}

Initialization

To include PubNub SDK in your project you need to use CocoaPods, install CocoaPods gem by following the procedure defined in the Getting Started guide. To add the PubNub SDK to your project with CocoaPods, there are four basic tasks to complete which are covered below:

  1. Create a new Xcode project.

  2. Create a Podfile in a new Xcode project root folder

    pod init
    platform :ios, '9.0'

    target 'application-target-name' do
    use_frameworks!

    pod "PubNub", "~> 4"
    end

If you have any other pods you'd like to include, or if you have other targets you'd to add (like a test target) add those entries to this Podfile as well. See the CocoaPods documentation for more information on Podfile configuration.

  • Install your pods by running pod install via the command line from the directory that contains your Podfile.
Note

After installing your Pods, you should only be working within the workspace generated by CocoaPods or specified by you in Podfile. Always open the newly generated workspace file, not the original project file!

To be able to use PubNub SDK within your application code you need to import it. Import PubNub SDK headers in implementation files for classes where you need to use it using this import statement:

#import <PubNub/PubNub.h>

Complete application delegate configuration

Add the PNObjectEventListener protocol to AppDelegate in implementation file to anonymous category:

Required UserId

Always set the userID to uniquely identify the user or device that connects to PubNub. This userID should be persisted, and should remain unchanged for the lifetime of the user or the device. If you don't set the userID, you won't be able to connect to PubNub.

#import <PubNub/PubNub.h>

@interface AppDelegate () <PNObjectEventListener>

// Stores reference on PubNub client to make sure what it won't be released.
@property (nonatomic, strong) PubNub *client;

@end

Description

This function is used for initializing the PubNub Client API context. This function must be called before attempting to utilize any API functionality in order to establish account level credentials such as publishKey and subscribeKey.

Method(s)

To Initialize PubNub you can use the following method(s) in the Objective-C SDK:

Initialize PubNub

+ (instancetype)clientWithConfiguration:(PNConfiguration *)configuration;
ParameterTypeRequiredDescription
configurationPNConfigurationYesReference on instance which store all user-provided information about how client should operate and handle events.

Initialize with callback queue

+ (instancetype)clientWithConfiguration:(PNConfiguration *)configuration callbackQueue:(dispatch_queue_t)callbackQueue;
ParameterTypeRequiredDescription
configurationPNConfigurationYesReference on instance which store all user-provided information about how client should operate and handle events.
callbackQueuedispatch_queue_tNoReference on queue which should be used by client for completion block and delegate calls.

Basic Usage

Initialize the PubNub client API

Required UserId

Always set the userID to uniquely identify the user or device that connects to PubNub. This userID should be persisted, and should remain unchanged for the lifetime of the user or the device. If you don't set the userID, you won't be able to connect to PubNub.

PNConfiguration *config = [PNConfiguration configurationWithPublishKey:@"demo"
subscribeKey:@"demo"
userID:@"myUserID"];
config.TLSEnabled = YES;
self.client = [PubNub clientWithConfiguration:configuration];

Returns

It returns the PubNub instance for invoking PubNub APIs like publish, subscribeToChannels, historyForChannel, hereNowForChannel, etc.

Other Examples

Initialize the client

Required UserId

Always set the userID to uniquely identify the user or device that connects to PubNub. This userID should be persisted, and should remain unchanged for the lifetime of the user or the device. If you don't set the userID, you won't be able to connect to PubNub.

PNConfiguration *configuration = [PNConfiguration configurationWithPublishKey:@"demo"
subscribeKey:@"demo"
userID:@"myUserID"];

self.client = [PubNub clientWithConfiguration:configuration];

Initialization for a Read-Only client

In the case where a client will only read messages and never publish to a channel, you can simply omit the publishKey when initializing the client:

Required UserId

Always set the userID to uniquely identify the user or device that connects to PubNub. This userID should be persisted, and should remain unchanged for the lifetime of the user or the device. If you don't set the userID, you won't be able to connect to PubNub.

PNConfiguration *configuration = [PNConfiguration configurationWithPublishKey:@""
subscribeKey:@"demo"
userID:@"myUserID"];
self.client = [PubNub clientWithConfiguration:configuration];

Event Listeners

You can be notified of connectivity status, message and presence notifications via the listeners.

Listeners should be added before calling the method.

Add Listeners

Listener's class should conform to PNEventsListener protocol to have access to available callbacks.

// Adding listener.
[pubnub addListener:self];

// Callbacks listed below.

- (void)client:(PubNub *)pubnub didReceiveMessage:(PNMessageResult *)message {
NSString *channel = message.data.channel; // Channel on which the message has been published
NSString *subscription = message.data.subscription; // Wild-card channel or channel on which PubNub client actually subscribed
NSNumber *timetoken = message.data.timetoken; // Publish timetoken
id msg = message.data.message; // Message payload
NSString *publisher = message.data.publisher; // Message publisher
}

- (void)client:(PubNub *)pubnub didReceiveSignal:(PNSignalResult *)signal {
NSString *channel = message.data.channel; // Channel on which the signal has been published
show all 102 lines

Remove Listeners

[pubnub removeListener:self]

Handling Disconnects

SDK performs automated re-connections and call status handler to report back. This will happen forever, but user may decide to stop it.

- (void)client:(PubNub *)pubnub didReceiveStatus:(PNStatus *)status {
if (status.isError && status.willAutomaticallyRetry) {
// Stop automatic retry attempts.
[status cancelAutomaticRetry];
}
}

Listener status events

CategoryDescription
PNAcknowledgmentCategoryReported operation request acknowledgment status.
PNAccessDeniedCategoryAccess Manager permission failure.
PNTimeoutCategoryServer didn't respond in time for reported operation request.
PNNetworkIssuesCategoryNo connection to Internet.
PNRequestMessageCountExceededCategoryThe SDK announces this error if requestMessageCountThreshold is set, and the number of messages received from PubNub (in-memory cache messages) exceeds the threshold.
PNConnectedCategoryThe SDK subscribed to new channels / channel groups.
PNReconnectedCategoryThe SDK was able to reconnect to PubNub.
PNDisconnectedCategoryThe SDK unsubscribed from channels / channel groups.
PNUnexpectedDisconnectCategoryThe SDK unexpectedly lost ability to receive live updated from PubNub.
PNRequestURITooLongCategoryReported operation request URI too long (too many channels / channel groups).
PNMalformedFilterExpressionCategoryThe SDK has been configured with malformed filtering expression.
PNMalformedResponseCategoryThe SDK received unexpected PubNub service response.
PNDecryptionErrorCategoryThe SDK unable to decrypt received message using configured cipherKey.
PNTLSConnectionFailedCategoryThe SDK unable to establish secured connection.
PNTLSUntrustedCertificateCategoryThe SDK unable to check certificates trust chain.

UserID

This function is used to set a user ID on the fly.

Method(s)

To set userID you can use the following method(s) in Objective-C SDK:

@property (nonatomic, copy, setter = setUserID:) NSString *userID;

Basic Usage

Required UserId

Always set the userID to uniquely identify the user or device that connects to PubNub. This userID should be persisted, and should remain unchanged for the lifetime of the user or the device. If you don't set the userID, you won't be able to connect to PubNub.

// User authorized and we need to update used UUID
PNConfiguration *configuration = self.client.currentConfiguration;
configuration.userID = @"myUniqueUserID";

__weak __typeof(self) weakSelf = self;
[self.client copyWithConfiguration:configuration completion:^(PubNub *client) {

weakSelf.client = client;
}];

Other Examples

Creating a function to subscribe a unique channel name

/**
Subscription process results arrive to listener which should adopt to
PNObjectEventListener protocol and registered using:
*/
[self.client addListener:self];
[self.client subscribeToChannels:@[[NSUUID UUID].UUIDString] withPresence:NO];

// Handle new message from one of channels on which client has been subscribed.
- (void)client:(PubNub *)client didReceiveMessage:(PNMessageResult *)message {

// Handle new message stored in message.data.message
if (![message.data.channel isEqualToString:message.data.subscription]) {

// Message has been received on channel group stored in message.data.subscription.
}
show all 77 lines

Creating a unique auth_key for Access Manager on initialization

PNConfiguration *configuration = self.client.currentConfiguration;
configuration.authKey = [NSUUID UUID].UUIDString.lowercaseString;

__weak __typeof(self) weakSelf = self;
[self.client copyWithConfiguration:configuration completion:^(PubNub *client) {

// Store reference on new client with updated configuration.
weakSelf.client = client;
}];

Authentication Key

Setter and getter for users auth key.

Method(s)

Authentication Key

@property (nonatomic, nullable, copy) NSString *authKey;

UserID

@property (nonatomic, copy, setter = setUserID:) NSString *userID;

Basic Usage

Set Auth Key

PNConfiguration *configuration = self.client.currentConfiguration;
configuration.authKey = @"my_new_authkey";

__weak __typeof(self) weakSelf = self;
[self.client copyWithConfiguration:configuration completion:^(PubNub *client) {

// Store reference on new client with updated configuration.
weakSelf.client = client;
}];

Get Auth Key

// Request current client configuration and pull out authorisation key from it.
NSString *authorizationKey = self.client.currentConfiguration.authKey;

Returns

Get Auth key returns the current authentication key.

Filter Expression

Requires Stream Controller add-on

This method requires that the Stream Controller add-on is enabled for your key in the Admin Portal. Read the support page on enabling add-on features on your keys.

Stream filtering allows a subscriber to apply a filter to only receive messages that satisfy the conditions of the filter. The message filter is set by the subscribing client(s) but it is applied on the server side thus preventing unwanted messages (those that do not meet the conditions of the filter) from reaching the subscriber.

To set or get message filters, you can use the following property. To learn more about filtering, refer to the Publish Messages documentation.

Method(s)

@property (nonatomic, nullable, copy) NSString *filterExpression;

Basic Usage

Set Filter Expression

Required UserId

Always set the userID to uniquely identify the user or device that connects to PubNub. This userID should be persisted, and should remain unchanged for the lifetime of the user or the device. If you don't set the userID, you won't be able to connect to PubNub.

PNConfiguration *configuration = [PNConfiguration configurationWithPublishKey:@"demo"
subscribeKey:@"demo"
userID:@"myUserID"];
self.client = [PubNub clientWithConfiguration:configuration];
self.client.filterExpression = @"(senderID=='PubNub')";

Get Filter Expression

NSLog(@"Filtering expression: %@", self.client.filterExpression);

Returns

Get Filter Expression returns the Current filtering expression.

Warning

If filter expression is malformed, PNObjectEventListener won't receive any messages and presence events from service (only error status).

Last updated on