let config = PNConfiguration(publishKey: "<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 = [.notifyAll]
self.client = PubNub.clientWithConfiguration(config)
self.client.addListener(self)

func client(_ client: PubNub, didReceive status: PNStatus) {
if status.operation == .heartbeatOperation {
/**
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/ios-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. */ }
}
}

PubNub client instance doesn’t allow dynamic configuration update, but it has helper functions which allow to receive new instance with updated configuration. Updated configuration will be applied on PubNub client copy. Original instance after copy block will return shouldn’t be used anymore.

Install the CocoaPods gem by following the procedure defined under How to Get It.

To add the PubNub SDK to your project with CocoaPods, there are four tasks to complete:

Create new Xcode project.

Create a Podfile in your newly created Xcode project root folder

touch Podfile

The PubNub client can be added as module (only with a deployment target of iOS 8.0 and above) or as a static library (for deployment targets of iOS 7.0 and above).

For a deployment target of iOS 8.0 and above use:

source 'https://github.com/CocoaPods/Specs.git'
# optionally complete and uncomment if compilation issues arise
# project '<path to project relative to this Podfile>/<name of project without extension>'
# workspace 'MyPubNubProject'
use_frameworks!
target 'application-target-name' do
# Should only use this with projects
# that must have a minimum deployment
# target of iOS 8
platform :ios, '8.0' # (or '9.0' or '10.0')
pod "PubNub", "~> 4"
end

For a deployment target of iOS 7.0 and above use:

source 'https://github.com/CocoaPods/Specs.git'
# optionally complete and uncomment if compilation issues arise
# project '<path to project relative to this Podfile>/<name of project without extension>'
# workspace 'MyPubNubProject'
target 'application-target-name' do
# Should only use this with projects
# that must have a minimum deployment
# target of iOS 7
platform :ios, '7.0' # (if you don't need to use iOS 7, then see other Podfile)
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.

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!

By creating the Podfile and running pod install in the same directory as your Podfile, you should now have a Swift project with the PubNub SDK installed. To access the PubNub Objective-C SDK with the deployment target set to iOS 7.0 and below, you will need to create a bridging header.

The bridging header is a way to make the Swift code aware of the available Objective-C interface to the PubNub client. To create your bridging header:

Create a new File (File → New → File) of type Source → Header File

Specify a location and name (along the lines of [MyProjectName]-Bridging-Header.h) and create the file

Make your project aware of this newly created bridging header by opening your project, selecting Build Settings, and using the search box to find Objective-C Bridging Header

Specify the path to the bridging header. The location is relative to the project / workspace files location.

The convention for bridging headers in Swift is to use a single bridging header file per project -- if you have other Pods included in your project that are written in Objective-C, they would be imported here as well.

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.

This examples demonstrates how to enable PubNub Transport Layer Encryption with SSL. Just initialize the client with ssl set to true. The hard work is done, now the PubNub API takes care of the rest. Just subscribe and publish as usual and you are good to go.

/**
Subscription process results arrive to listener which should adopt to PNObjectEventListener protocol
and registered using:
*/
self.client.addListener(self)
self.client.subscribeToChannels([NSUUID().uuidString], withPresence: false)
// Handle new message from one of channels on which client has been subscribed.
func client(_ client: PubNub, didReceiveMessage message: PNMessageResult) {
// Handle new message stored in message.data.message
if message.data.channel != message.data.subscription {
// Message has been received on channel group stored in message.data.subscription.
}
else {
// Message has been received on channel stored in message.data.channel.
}
print("Received message: \(message.data.message) on channel \(message.data.channel) " +
"at \(message.data.timetoken)")
}
// Handle subscription status change.
func client(_ client: PubNub, didReceive status: PNStatus) {
if status.operation == .subscribeOperation {
// Check whether received information about successful subscription or restore.
if status.category == .PNConnectedCategory || status.category == .PNReconnectedCategory {
let subscribeStatus: PNSubscribeStatus = status as! PNSubscribeStatus
if subscribeStatus.category == .PNConnectedCategory {
// This is expected for a subscribe, this means there is no error or issue whatsoever.
}
else {
/**
This usually occurs if subscribe temporarily fails but reconnects. This means there was
an error but there is no longer any issue.
*/
}
}
else if status.category == .PNUnexpectedDisconnectCategory {
/**
This is usually an issue with the internet connection, this is an error, handle
appropriately retry will be called automatically.
*/
}
// Looks like some kind of issues happened while client tried to subscribe or disconnected from
// network.
else {
let errorStatus: PNErrorStatus = status as! PNErrorStatus
if errorStatus.category == .PNAccessDeniedCategory {
/**
This means that PAM does allow this client to subscribe to this channel and channel group
configuration. This is another explicit error.
*/
}
else {
/**
More errors can be directly specified by creating explicit cases for other error categories
of `PNStatusCategory` such as: `PNDecryptionErrorCategory`,
`PNMalformedFilterExpressionCategory`, `PNMalformedResponseCategory`, `PNTimeoutCategory`
or `PNNetworkIssuesCategory`
*/
}
}
}
}

let config = PNConfiguration(publishKey: "<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 = [.notifyAll]
self.client = PubNub.clientWithConfiguration(config)
self.client.addListener(self)

func client(_ client: PubNub, didReceive status: PNStatus) {
if status.operation == .heartbeatOperation {
/**
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/ios-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. */ }
}
}

PubNub client instance doesn’t allow dynamic configuration update, but it has helper functions which allow to receive new instance with updated configuration. Updated configuration will be applied on PubNub client copy. Original instance after copy block will return shouldn’t be used anymore.

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.

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!

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.

This examples demonstrates how to enable PubNub Transport Layer Encryption with SSL. Just initialize the client with ssl set to true. The hard work is done, now the PubNub API takes care of the rest. Just subscribe and publish as usual and you are good to go.

/**
Subscription process results arrive to listener which should adopt to PNObjectEventListener protocol
and registered using:
*/
self.client.addListener(self)
self.client.subscribeToChannels([NSUUID().uuidString], withPresence: false)
// Handle new message from one of channels on which client has been subscribed.
func client(_ client: PubNub, didReceiveMessage message: PNMessageResult) {
// Handle new message stored in message.data.message
if message.data.channel != message.data.subscription {
// Message has been received on channel group stored in message.data.subscription.
}
else {
// Message has been received on channel stored in message.data.channel.
}
print("Received message: \(message.data.message) on channel \(message.data.channel) " +
"at \(message.data.timetoken)")
}
// Handle subscription status change.
func client(_ client: PubNub, didReceive status: PNStatus) {
if status.operation == .subscribeOperation {
// Check whether received information about successful subscription or restore.
if status.category == .PNConnectedCategory || status.category == .PNReconnectedCategory {
let subscribeStatus: PNSubscribeStatus = status as! PNSubscribeStatus
if subscribeStatus.category == .PNConnectedCategory {
// This is expected for a subscribe, this means there is no error or issue whatsoever.
}
else {
/**
This usually occurs if subscribe temporarily fails but reconnects. This means there was
an error but there is no longer any issue.
*/
}
}
else if status.category == .PNUnexpectedDisconnectCategory {
/**
This is usually an issue with the internet connection, this is an error, handle
appropriately retry will be called automatically.
*/
}
// Looks like some kind of issues happened while client tried to subscribe or disconnected from
// network.
else {
let errorStatus: PNErrorStatus = status as! PNErrorStatus
if errorStatus.category == .PNAccessDeniedCategory {
/**
This means that PAM does allow this client to subscribe to this channel and channel group
configuration. This is another explicit error.
*/
}
else {
/**
More errors can be directly specified by creating explicit cases for other error categories
of `PNStatusCategory` such as: `PNDecryptionErrorCategory`,
`PNMalformedFilterExpressionCategory`, `PNMalformedResponseCategory`, `PNTimeoutCategory`
or `PNNetworkIssuesCategory`
*/
}
}
}
}