Skip to content

About

PubNub Objective-C based APIs for iOS and OS X

Topics

Resources

Stars

190 stars

Watchers

49 watching

Forks

Repository files navigation

objective-c-header

PubNub Objective-C SDK

CocoaPods

PubNub provides global infrastructure for real-time, interactive applications.

Publish and receive messages in Objective-C. Use this SDK for Objective-C applications on supported Apple platforms.

Documentation · API reference · Changelog · Support

Requirements

Requirement Supported version or setup
Xcode / Objective-C Use the Apple platform versions listed in Platform Support
Apple platforms iOS, macOS, tvOS, and watchOS targets supported by the SDK
CocoaPods Add PubNub 7.x to the Podfile, run pod install, and open the generated .xcworkspace.

Installation

Add the following to the application's Podfile:

pod 'PubNub', '~> 7.0'

For other installation methods, see environment setup.

Quickstart

This example runs in an Objective-C iOS application. It subscribes to hello_world, publishes one message, and prints the received text.

Get your keys

  1. Open the PubNub Admin Portal.
  2. Create an app and a keyset for development, or select an existing development keyset.
  3. Copy its publish key and subscribe key.

If Access Manager is enabled, obtain a token from your trusted backend and call [client setAuthToken:token]. Keep the secret key on the backend.

Send and receive a message

Add the following to ViewController.m and make the view controller conform to PNEventsListener:

Replace the key placeholders. Use a User ID that identifies the user or device in your app.

#import "ViewController.h"
#import <PubNub/PubNub.h>

@interface ViewController () <PNEventsListener>
@property (nonatomic, strong) PubNub *client;
@property (nonatomic, assign) BOOL published;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    PNConfiguration *configuration =
        [PNConfiguration configurationWithPublishKey:@"YOUR_PUBLISH_KEY"
                                        subscribeKey:@"YOUR_SUBSCRIBE_KEY"
                                              userID:@"hello-world-user"];

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

    PNSubscribeRequest *request = [PNSubscribeRequest requestWithChannels:@[@"hello_world"]
                                                            channelGroups:nil];
    [self.client subscribeWithRequest:request];
}

- (void)client:(PubNub *)client didReceiveStatus:(PNStatus *)status {
    if (status.category == PNConnectedCategory && !self.published) {
        self.published = YES;

        PNPublishRequest *request = [PNPublishRequest requestWithChannel:@"hello_world"];
        request.message = @"Hello world";
        [client publishWithRequest:request completion:^(PNPublishStatus *publishStatus) {
            if (publishStatus.isError) {
                NSLog(@"Publish failed: %@", publishStatus.errorData.information);
            }
        }];
    }
}

- (void)client:(PubNub *)client didReceiveMessage:(PNMessageResult *)message {
    NSLog(@"%@", message.data.message);

    PNPresenceLeaveRequest *request = [PNPresenceLeaveRequest requestWithChannels:@[@"hello_world"]
                                                                    channelGroups:nil];
    [client unsubscribeWithRequest:request];
    [client removeListener:self];
}

@end

Run the example

Run the application target from Xcode.

The Xcode console should show:

Hello world

The example calls unsubscribeWithRequest: and removeListener: after receiving the message. Use [client unsubscribeFromAll] when all subscriptions should stop.

For a complete application, see the getting started guide.

Next steps

Task Documentation
Configure the client Configuration
Work with subscriptions and messages Publish & Subscribe
Check channel occupancy Presence
Read message history Message Persistence

Build with an AI coding assistant

The PubNub MCP server gives an AI coding assistant access to PubNub SDK documentation and PubNub APIs. Connect the assistant to the hosted server at https://mcp.pubnub.com, or run npx @pubnub/mcp@latest locally.

The server repository has setup steps for VS Code, Cursor, Claude Code, Claude Desktop, Codex, Gemini CLI, and OpenCode.

Before production

Use Access Manager to grant each client the permissions it needs. Keep the secret key on your backend. Never include it in a distributed client.

Reuse the PubNub client for the application/session. Unsubscribe channels that are no longer needed, use [client unsubscribeFromAll] to stop every subscription, and remove listeners with removeListener: when their owner is released.

Live delivery through PubNub SDKs is at-most-once. A subscriber can miss messages while disconnected or if its buffer overflows. For longer-gap recovery, see Message Persistence.

Troubleshooting

Symptom Check
Xcode cannot import PubNub after CocoaPods installation Run pod install and open the generated .xcworkspace, not the original .xcodeproj. See the Objective-C SDK guide.
A publish succeeds but no message appears Check the message handler, subscription readiness, keyset, and channel name.
pod install fails because the target's deployment target is lower than the PubNub minimum Raise the platform version in the Podfile and the Xcode deployment target to the minimum listed in Platform Support.
Extra clients or duplicate messages during development Keep one client/listener set for the owning application component. Unsubscribe and remove listeners during lifecycle cleanup instead of accumulating listeners as controllers are recreated.

For setup help, see troubleshooting. Check network status for service incidents.

Releases

Read the changelog before upgrading.

For a major version change, review the Objective-C SDK changelog for breaking changes before upgrading.

Support and contributions

For setup or account help, contact PubNub Support.

For a reproducible SDK bug, use GitHub Issues. Include the SDK version, runtime, and a small reproduction with credentials removed.

Build the supported Apple targets, run the repository test suite, and include tests for behavioral changes before opening a pull request.

License

See the PubNub Software Development Kit License Agreement.

About

PubNub Objective-C based APIs for iOS and OS X

Topics

Resources

Stars

190 stars

Watchers

49 watching

Forks

Releases

Packages

Used by

Contributors

Languages