Free up to 1MM monthly messages. No credit card required.
Welcome 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.
Let’s get started!
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.
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:
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.
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:
Go into your
AppDelegate.swift and in
didFinishLaunchingWithOptions function add the following:
And last add the following function beneath:
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.
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”.
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
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.
Comparing the major game engines: Unity vs Unreal Engine vs Corona SDK vs GameMaker Studio, including the benefits and cons of...
Sockets (aka socket programming) enable programs to send and receive data, bi-directionally, at any given moment. This tutorial...