This is the official PubNub Objective-C SDK repository.
PubNub takes care of the infrastructure and APIs needed for the realtime communication layer of your application. Work on your app's logic and let PubNub handle sending and receiving data across the world in less than 100ms.
You will need the publish and subscribe keys to authenticate your app. Get your keys from the Admin Portal.
Install the latest
cocoapodsgem by running thegem install cocoapodscommand. If you already have this gem, make sure to update to the latest version by running thegem update cocoapodscommand.Create a new Xcode project and create a
Podfilein the root folder of the project:pod initplatform :ios, '9.0' target 'application-target-name'do use_frameworks! pod "PubNub", "~> 4" end
If you want to include additional pods or add other targets, add their entries to this Podfile as well. Refer to the CocoaPods documentation for more information on Podfile configuration.
Install your pods by running the
pod installcommand from the directory which contains your Podfile. After installing your Pods, you should work with the CocoaPods-generated workspace and not the original project file.Import the PubNub headers in the classes where you want to use PubNub:
#import<PubNub/PubNub.h>
Configure your keys:
// Initialize and configure PubNub client instance PNConfiguration *configuration = [PNConfiguration configurationWithPublishKey:@"myPublishKey"subscribeKey:@"mySubscribeKey"]; configuration.uuid = @"myUniqueUUID"; self.client = [PubNub clientWithConfiguration:configuration];
// Listener's class should conform to `PNEventsListener` protocol// in order 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 publishedNSString *subscription = message.data.subscription; // Wild-card channel or channel on which PubNub client actually subscribedNSNumber *timetoken = message.data.timetoken; // Publish timetokenid msg = message.data.message; // Message payloadNSString *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 publishedNSString *subscription = message.data.subscription; // Wild-card channel or channel on which PubNub client actually subscribedNSNumber *timetoken = message.data.timetoken; // Signal timetokenid msg = message.data.message; // Signal payloadNSString *publisher = message.data.publisher; // Signal publisher } - (void)client:(PubNub *)pubnub didReceiveMessageAction:(PNMessageActionResult *)action{NSString *channel = action.data.channel; // Channel on which the message has been publishedNSString *subscription = action.data.subscription; // Wild-card channel or channel on which PubNub client actually subscribedNSString *event = action.data.event; // Can be: added or removedNSString *type = action.data.action.type; // Message action typeNSString *value = action.data.action.value; // Message action valueNSNumber *messageTimetoken = action.data.action.messageTimetoken; // Timetoken of the original messageNSNumber *actionTimetoken = action.data.action.actionTimetoken; // Timetoken of the message actionNSString *uuid = action.data.action.uuid; // UUID of user which added / removed message action } - (void)client:(PubNub *)pubnub didReceivePresenceEvent:(PNPresenceEventResult *)event{NSString *channel = message.data.channel; // Channel on which presence changesNSString *subscription = message.data.subscription; // Wild-card channel or channel on which PubNub client actually subscribedNSString *presenceEvent = event.data.presenceEvent; // Can be: join, leave, state-change, timeout or intervalNSNumber *occupancy = event.data.presence.occupancy; // Number of users subscribed to the channel (not available for state-change event)NSNumber *timetoken = event.data.presence.timetoken; // Presence change timetokenNSString *uuid = event.data.presence.uuid; // UUID of user for which presence change happened// Only for 'state-change' eventNSDictionary *state = event.data.presence.state; // User state (only for state-change event)// Only for 'interval' eventNSArray<NSString *> *join = event.data.presence.join; // UUID of users which recently joined channelNSArray<NSString *> *leave = event.data.presence.leave; // UUID of users which recently leaved channelNSArray<NSString *> *timeout = event.data.presence.timeout; // UUID of users which recently timed out on channel } - (void)client:(PubNub *)pubnub didReceiveObjectEvent:(PNObjectEventResult *)event{NSString *channel = event.data.channel; // Channel to which the event belongsNSString *subscription = event.data.subscription; // Wild-card channel or channel on which PubNub client actually subscribedNSString *event = event.data.event; // Can be: set or deleteNSString *type = event.data.type; // Entity type: channel, uuid or membershipNSNumber *timestamp = event.data.timestamp; // Event timestamp PNChannelMetadata *channelMetadata = event.data.channelMetadata; // Updated channel metadata (only for channel entity type) PNUUIDMetadata *uuidMetadata = event.data.uuidMetadata; // Updated channel metadata (only for uuid entity type) PNMembership *membership = event.data.membership; // Updated channel metadata (only for membership entity type) } - (void)client:(PubNub *)pubnub didReceiveFileEvent:(PNFileEventResult *)event{NSString *channel = event.data.channel; // Channel to which file has been uploadedNSString *subscription = event.data.subscription; // Wild-card channel or channel on which PubNub client actually subscribedid message = event.data.message; // Message added for uploaded fileNSString *publisher = event.data.publisher; // UUID of file uploaderNSURL *fileDownloadURL = event.data.file.downloadURL; // URL which can be used to download fileNSString *fileIdentifier = event.data.file.identifier; // Unique file identifierNSString *fileName = event.data.file.name; // Name with which file has been stored remotely } - (void)client:(PubNub *)pubnub didReceiveStatus:(PNStatus *)status{PNStatusCategory category = status.category; // One of PNStatusCategory fields to identify status of operation processing PNOperationType operation = status.operation; // One of PNOperationType fields to identify for which operation status receivedBOOL isError = status.isError; // Whether any kind of error happened.NSInteger statusCode = status.statusCode; // Related request processing status codeBOOL isTLSEnabled = status.isTLSEnabled; // Whether secured connection enabledNSString *uuid = status.uuid; // UUID which configured for passed clientNSString *authKey = status.authKey; // Auth key configured for passed clientNSString *origin = status.origin; // Origin against which request has been sentNSURLRequest *clientRequest = status.clientRequest; // Request which has been used to send last request (may be nil)BOOL willAutomaticallyRetry = status.willAutomaticallyRetry; // Whether client will try to perform automatic retry// Following is available when operation == PNSubscribeOperation,// because status is PNSubscribeStatus instance in this case PNSubscribeStatus *subscribeStatus = (PNSubscribeStatus *)status; NSNumber *currentTimetoken = subscribeStatus.currentTimetoken; // Timetoken which has been used for current subscribe requestNSNumber *lastTimeToken = subscribeStatus.lastTimeToken; // Timetoken which has been used for previous subscribe requestNSArray<NSString *> *subscribedChannels = subscribeStatus.subscribedChannels; // List of channels on which client currently subscribedNSArray<NSString *> *subscribedChannelGroups = subscribeStatus.subscribedChannelGroups; // List of channel groups on which client currently subscribedNSString *channel = subscribeStatus.data.channel; // Name of channel to which status has been receivedNSString *subscription = subscribeStatus.data.subscription; // Wild-card channel or channel on which PubNub client actually subscribedNSNumber *timetoken = subscribeStatus.data.timetoken; // Timetoken at which event arrivedNSDictionary *userMetadata = subscribeStatus.data.userMetadata; // Metadata / envelope which has been passed along with event// Following is available when isError == YES,// because status is PNErrorStatus instance in this case PNErrorStatus *errorStatus = (PNErrorStatus *)status; id associatedObject = errorStatus.associatedObject; // Data which may contain related information (not decrypted message for example)NSArray<NSString *> *erroredChannels = errorStatus.errorData.channels; // List of channels for which error reported (mostly because of PAM)NSArray<NSString *> *erroredChannelGroups = errorStatus.errorData.channelGroups; // List of channel groups for which error reported (mostly because of PAM)NSString *errorInformation = errorStatus.errorData.information; // Stringified information about errorid errorData = errorStatus.errorData.data; // Additional error information from PubNub service }[self.client publish:@{@ "msg": @"hello" } toChannel:targetChannel withCompletion:^(PNPublishStatus *publishStatus){if (!publishStatus.isError){// Message successfully published to specified channel. } else{/** * Handle message publish 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: [publishStatus retry];*/ } }]; [self.client subscribeToChannels: @[@"hello-world-channel"] withPresence:YES];If you need help or have a general question, contact [email protected].
The PubNub Swift SDK is released under the PubNub Software Development Kit License.
See LICENSE for details.