Free up to 1MM monthly messages. No credit card required.
We know the world’s going mobile. As a result, mobile app development is growing exponentially in demand. However, there are hundreds of platforms to choose from, and mobile development for a number of different platforms is pricey.
Luckily, there is a solution which allows you to develop for multiple mobile platforms with one codebase. This solution is PhoneGap/Cordova.
And luckily for you, the same code can be used on other platforms (check out the Android version of this demo here).
By using HTML and AngularJS we will build a chat you can run on multiple devices with different screen sizes and resolutions. Example devices are the Nexus 5, Nexus 6, Motorola’s Moto X or Moto G, and any of Samsung’s or LG’s flagship phones.
In this post we will use the Cordova open-source engine to build an iOS specific app. Because PhoneGap uses more or less the same engine, we will use the two terms interchangeably.
Let’s dive in and figure out all we need before getting started!
That’s all! For the specific mobile platforms you want to compile your code to, it may take a little more time if you don’t already have all the tools setup. But honestly, it won’t be too troublesome installing all of it.
$npm install -g ios-simin the terminal should do the trick)
Alright, at this point if you already had your environment set up you just needed to write a few words in the terminal. If you had to install Xcode, it probably took you a lot more time than that.
Now that we’re all up to speed, let’s start a Cordova project. To do so, let’s
cd to the working directory of our choice, and run the following command:
The folder name is the only required field here, but you’ll be better off inputing all three of them right now. The second field,
com.example.appname represents the package identifier as you’d name it in a Xcode project for example.
Cordova simply built the structure for a cordova project with a little HelloWorld app. Let’s look at the folders we created:
You can probably keep the folders and delete all the files inside our
www/ folder, we won’t be needing the given helloWorld app, but feel free to take a look at it.
You do want to keep the config.xml file in the root directory and edit the relevant points (change the app name, add a description, put in the author’s name, etc…)
In this section, you will find all of the required code to make the app run. To set up the structure of your simple AngularJS chat application, check out our introduction to AngularJS chat application blog post here. We go line-by-line!
It’s important to remember that building mobile apps in Cordova allows you to create web applications as native applications. As a result, developing mobile applications in Cordova resembles developing web applications for mobile browsers.
The head of our document will be as follows:
This looks familiar. As expected we start by defining our encryption. The rest is less common:
"format-detection"meta tag simply disables the ability for telephone numbers to appear as hypertext links. The default value for iOS and Safari being yes, we have decided to disable it so that all devices behave similarly.
"msapplication-tap-highlight"meta tag is a Microsoft only tag disabling tap highlighting for links.
"viewport"tag is the most important here as it defines the behavior of our view. It is set here such that the user may not use zoom functionalities and sets the scale of the page. Redundantly we limit the maximum and minimum scales to 1. Feel free to modify them as you wish. We also set the canvas of our HTML code to match the screen of the device. Finally, we set the pixel density. This is a more delicate affair and might need to be tweaked depending on the device. Remember that a pixel is not a pixel! Understand that a screen pixel and a CSS pixel are different entities. More about this tag on Mozilla’s and Safari’s developer page.
Let’s do a rapid overview of the scripts:
Let’s make it simple. Angular is the key to our app’s dynamism thanks to the ng-app directive referring to our module and ng-controller directive which links our controller to our template.
We can divide the code into two key parts. One listing the Users, one listing the messages. Let’s have a quick review of these angular attributes:
Finally, we call our cordova script which is essential for our app to function. The
"pubnubAngularApp.js" holds the brain of our app!
Let’s open the hood of our app’s downloadable example code. We’ll explore the fun logic inside before taking it for a ride on real devices or emulators.
You’ll want to sign up for a free PubNub account, then get your unique publish/subscribe keys from the Developer Admin Dashboard. We have an unlimited Sandbox tier, so test as much as you like. Input your publish/subscribe keys below:
The controller first initialized PubNub with a subscribe and publish key.
Once this is done, we can subscribe to a channel where all the users from the chat will send their messages.
Because we want to upload these messages to the screen as soon as they are received on the channel, we write a callback which is triggered every time the new message event is fired.
We build similar methods to when a new user appears by handling a presence event.
We can also view the history of the channel to repopulate the previous messages sent and users list.
Finally, we create the
publish() method which is called whenever a user hits the send button.
Voilà! We’re done! All we want is to see the app run on all our devices.
What we’ll want to do is follow 3 steps:
These lines simply create the proper platform set up for the application and builds our app for a given platform.
This simple line will run the default ios emulator you use with Xcode.
Building the cordova code created a full Xcode project. You can simply open this project and run it on your iOS like you normally would on Xcode. Remember that you need to sign your code and have a Apple Developer ID to do so!
As we said before, we also wrote this tutorial for building an Android chat app using AngularJS and PhoneGap. You can also check out a number of our iOS projects and tutorials here.
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...