Build

Get Started: Swift PubNub Storage & Playback and Presence

4 min read Michael Carroll on Jul 1, 2015

swift pubnub sdk real-time ios apiWelcome back to our quick and easy tutorial for getting started using the Swift and PubNub. In our first blog post, we got publish/subscribe messaging for Swift set up, and we recommend starting there, then returning here.

In this post, we’ll show you how to get Storage & Playback and Presence up and running with Swift for PubNub.

And luckily for you, we just released our brand new and improved iOS SDK. Check it out here.

Let’s get started!

Creating A New Project

First, open Xcode and Go To: File -> New -> Project

Select a Application Type and press Next.

Enter your App Name for the Product Name, set the Language to Swift, and Devices to iPhone and click Next.

Choose a directory to save your project, and click Create.

Installing CocoaPods

If you have never installed a Pod before, a really good tutorial on how to get started can be found the CocoaPods official website at:

https://guides.cocoapods.org/using/using-cocoapods.html

Once you have CocoaPods installed go ahead and open the PodFile for YourProject by going into your project’s root directory and typing:

and paste the following into your pod file:

Make sure you grab the most recent pod versions in your project.

Now just go ahead and update then install the pod by typing:

Also, it is important that after installing our pods here on out we operate entirely in the Workspace part of our project.

Adding a Bridging-Header

The next thing we need to do to connect our Swift app to our pods is create an Objective-C bridging header. To do this you need to create a new File (File -> New -> File) of type Objective-C File. Call this whatever you like because we will end up deleting this file.

Here I have just called it testHeader. When you see “Would you like to configure an Objective-C bridging header?” select Yes. This is the file we really care about, so you can delete the .m file you just created.

Now it will add some new files to your project. Click on the File: YourProject-Bridging-Header.h. Underneath the commented code we need to add an #import to our project to use the PubNub iOS SDK. To do this we simply add the following lines into this file:

Basic Publish/Subscribe with Swift

data-stream-network

Go into your AppDelegate.swift and in didFinishLaunchingWithOptions function add the following:

And last add the following function beneath:

Thats it!

Go ahead and run your project, and visit your PubNub Dev Console to check out what you’ve done. You should see “Swift + PubNub!” has been published, and if you try publishing from the Dev Console you’ll see the message is received in your Xcode console.

Now let’s move onto some more advanced PubNub-ery, implementing Storage & Playback and Presence.

How to Set Up Presence

presence

PubNub Presence lets you track online and offline status of users and devices in real time. It’s essentially a ‘buddy list’ for any connected device.

To set up presence notifications for a specific channel we only need to add about two more lines of code. First, in our didFinishLaunchingWithOptions we now add the following:

We are now subscribed to “Your_Channel” for presence notifications. The next thing we need to do is handle those notifications as we receive them. Create a new function in the same file and set it up like so:

Now if we go ahead and run the project we should see information about presence being printed to the console. To further test this you can go to your PubNub Dev Console and toggle subscribing and unsubscribing to “Your_Channel”.

Basic Storage & Playback (aka ‘history’) Requests

storage-playback

PubNub Storage and Playback lets you store real-time data streams and retrieve and playback them as they happened. Setting up Storage & Playback is useful for many applications where retrieving old publishes is valuable. We can add History to our channel simply by adding the following into our didFinishLaunchingWithOptions:

If you run your project you will see the entire history of your gets channel printed in your console! By adding start: nil, end: nil we are retrieving the entire history on the channel. You can also set specific bounds for your history request if you do not need the entire history.

That’s all for now. Stay tuned for future PubNub iOS and Swift demos!

0