Objective-C API & SDK Docs 7.1.0
This guide walks you through a simple "Hello, World" application that demonstrates the core concepts of PubNub. You'll set up a connection to PubNub and send and receive messages.
Setup
Get your PubNub keys
First, get your PubNub keys:
- Sign in or create an account on the PubNub Admin Portal.
- Create an app (or use an existing one).
- Find your publish and subscribe keys in the app dashboard.
When you create an app, PubNub automatically generates a keyset. You can use the same keyset for development and production, but we recommend separate keysets for each environment to improve security and management.
Install the SDK
SDK version
Always use the latest SDK version to have access to the newest features and avoid security vulnerabilities, bugs, and performance issues.
Download the SDK from any of the following sources:
Use CocoaPods
To integrate PubNub into your project using CocoaPods
-
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 Podfile in the root folder of the project:
1pod init1platform :ios, '14.0'
2
3 target 'application-target-name' do
4 use_frameworks!
5
6 pod "PubNub", "~> 5"
7 endIf 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:
1#import <PubNub/PubNub.h>
Use Carthage
To build PubNub as a standalone bundle using Carthage and integrate it into your project:
-
Install the latest release of Carthage and either create a new file or open an existing Cartfile.
-
Add a new line in the Cartfile to build the PubNub framework bundle:
1github "pubnub/objective-c" ~> 4 -
Update and rebuild your project's dependencies.
The
updatecommand ensures that the latest PubNub client is used to build the framework. You can also use the fasterbuildcommand if you have already built the framework before.1carthage update --no-use-binariesThe preceding command builds the framework for all configured platforms. You can use the following command to specify the platform:
1carthage update --platform ios --no-use-binariesAvailable platforms include
mac,tvos, andwatchos. -
Navigate to
Carthage/Buildand enter the directory which represents your target platform, for example:iOS. -
Drag and drop the
PubNub.frameworkbundle from theProductsdirectory to your application. -
Mark the Copy items if needed checkbox and click Finish.
-
Open your project's General tab, scroll to Embedded Binaries, click +, and select the
PubNub.frameworkfile. -
At this point, you should have the framework added to your application project. Import the PubNub headers in the classes where you want to use PubNub:
1#import <PubNub/PubNub.h>
Source code
Clone the GitHub repository:
1git clone https://github.com/pubnub/objective-c
View the supported platforms.
Steps
Initialize PubNub
You will use the workspace you generated with CocoaPods in this procedure.
-
In Xcode, open your workspace and add the following content to the
AppDelegateclass.1@interface AppDelegate () <PNEventsListener>
2
3// Stores reference on PubNub client to make sure what it won't be released.
4@property (nonatomic, strong) PubNub *client;
5
6@end -
Below what you added in step 1, add the following code.
This is the minimum configuration you need to send and receive messages with PubNub.
Make sure to replace myPublishKey and mySubscribeKey with your app's publish and subscribe keys from the Admin Portal.
1// Initialize and configure PubNub client instance
2PNConfiguration *configuration = [PNConfiguration configurationWithPublishKey: @"myPublishKey" subscribeKey:@"mySubscribeKey"];
3configuration.uuid = @"myUniqueUUID";
4
5self.client = [PubNub clientWithConfiguration:configuration];
For more information, refer to the Configuration section of the SDK documentation.