-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating
Phonegap Essentials
By :
Since this book covers all major operating system platforms, before we start to learn more about PhoneGap and its useful features we need to prepare the development environment. The PhoneGap website (http://phonegap.com/install/) has instructions on how to install it, which should be fairly easy to achieve. However, in reality, each platform requires us to install additional libraries and simulators to be able to run them for different mobile platforms.
There are two ways to develop PhoneGap applications. Until PhoneGap introduced the command-line interface (CLI), the only way was to develop plugins for the integrated development environment (IDE), such as Xcode or Eclipse. With the introduction of the CLI, it was possible to develop PhoneGap mobile applications with PhoneGap SDK inside your favorite text editor or integrated developer environment. In this book, we are going to use the latter option; since most developers have different opinions about editors, it is better to stay neutral on this topic.
However, for this book I will be using Sublime Text 3 (http://www.sublimetext.com/3), a plain text editor that is one of the most popular choices nowadays and has numerous plugins and extensions to work with. Since it supports Windows, Mac OS, and Linux, it won't be included in the installation process for a specific operating system. On the provided link, you can download the text editor and install it with the provided information. Sublime Text 3 can be used for free for as long as you like (you will be prompted to buy it from time to time); this is a fully working editor without any limits. But I would strongly advise you to get the full version and support the people behind it since they have done a great job and it only costs $70 and doesn't have restrictions on the number of machines for a single user.
There is a great free alternative to Sublime Text called Atom editor, which is open source and available at the following page: https://atom.io/. It will be sufficient to do the work but it doesn't come with lot of plugins like Sublime Text.
In the following pages, there will be instructions on how to cover the installation process for the three most popular operating systems that developers are using: Windows, Mac OS, and Ubuntu. For other Linux distributions, it should not be too hard to adapt the Ubuntu installation process, especially if you are using Debian. Let us get started.
In the beginning, PhoneGap started as a way to create iOS applications with JavaScript and HTML to create a fully functional iOS application without writing a single line of code in Objective-C. To get started for Mac OS, there are few more steps than the instructions on the PhoneGap website (http://phonegap.com/install/) are telling us to perform. We will need to install simulators for supported mobile devices. On this platform, we will install support for iOS and Android development. For installation purposes, it is assumed that this is being installed as a clean installation so, if you already have some of the libraries installed, it is advisable to update them to the latest version to make sure they are working properly. For Mac OS, the latest operating system version (Yosemite) will be used.
The easiest way to install the various Unix tools and open source software onto Mac OS X is via a package manager but unfortunately OS X does not come with one; however, there are some alternatives that we can use instead. We are going to use Homebrew (http://brew.sh/).
If you already have your own package manager preference or you want to compile it from the source code, you are encouraged to use the latest version for the library.
Before installing Homebrew, we need to install Xcode (https://itunes.apple.com/au/app/xcode/id497799835?mt=12) so we can build and run applications in the iOS simulator:

To install the Xcode component, perform the following steps:
Xcode icon into the Applications folder. Before proceeding with the next step, you need to open Xcode and agree to the license, as it needs to install additional components:
xcode-select —install

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
brew doctor
update to get a list of the latest versions of the libraries:brew update
brew install node
brew install android-sdk
vi ~/.bash_profile
export ANDROID_HOME=`brew --prefix android`
source ~/.bash_profile
android sdk


brew install ant
android avd


sudo npm install -g ios-sim
sudo npm install -g PhoneGap
PhoneGap create hello com.example.hello HelloWorld cd hello
phonegap run ios --emulator

phonegap run android —emulator

Linux is well known for having multiple distributions that have different ways of installing new libraries on it. For this book, we are going to use one of the most popular distributions: Ubuntu. Ubuntu is built on top of Debian so installing on both of them shouldn't be much different. As for the other distributions, it shouldn't be too hard to adapt the following commands for these environments. Perform the following steps:
sudo apt-get update sudo apt-get install build-essential
sudo apt-get install python-software-properties python g++ make
sudo apt-get install nodejs
nodejs. The problem is that many applications, including PhoneGap, expect the executable to be named node. To fix this inconsistency, simply create a symlink named node that points to Node.js as follows:sudo ln -s /usr/bin/nodejs /usr/bin/node
sudo apt-get install npm
sudo npm install -g phonegap
sudo apt-get install ant
sudo apt-get install openjdk-7-jre sudo apt-get install openjdk-7-jdk
wget http://dl.google.com/android/android-sdk_r24.0.2-linux.tgz tar zxvf android-sdk_r24.0.2-linux.tgz
sudo mv android-sdk-linux/ /opt/android/
vi editor:vi ~/.bashrc
export ANDROID_HOME="/usr/local/android-sdk-linux/tools" export ANDROID_PLATFORM_TOOLS="/usr/local/android-sdk-linux/platform-tools" export PATH="$PATH:$ANDROID_HOME:$ANDROID_PLATFORM_TOOLS"
source ~/.bashrc
android
If there is no error, you should see that executing Android opens up a kind of package manager that you can use to install different Android components. By default, a certain number of these will be pre-selected for installation. Leave them as is and, in addition, tick the box next to the entry named API 4.4.2 (API 19). Once you've done that, download and install everything by accepting the license, as shown in the following screenshot:


android avd


phonegap create hello com.example.hello HelloWorld cd hello
phonegap run android —emulator
If you want to develop and test applications for Windows phones, you will need to have a working Windows computer to hand because you can only run them there.
There are multiple versions of the Windows operating system but it should be fairly easy to use any of them by carrying out the following instructions, since most of the libraries and applications are available as an installer. Thus, you can simply go ahead with the installation process and the only thing you need to do manually is to set the environment variables to be reachable from any command prompt.
The easiest way is to create a Development directory in your home directory (C:\Users\yourusername\Development). For ease, you might want to drag that folder into the Favorites list in Explorer, too. We will add all the source code we work on to the examples there.
Let us start with the stable Java SE Development Kit. Perform the following steps:


You will need to press the Environment Variables button and update system variables with the following data:
JAVA_HOME with value: C:\Program Files\Java\jdk1.7.0_75
Path and add the following value ;%JAVA_HOME%\bin
; (semicolon). It is important not to forget it:
java
C:\Android\android-sdk.
ANDROID_HOME with value C:\Android\android-sdk
Path and append additional value ;%ANDROID_HOME%\platform-tools;%ANDROID_HOME%\tools
; (semicolon) at the beginning.android

We need to install the git command (used for many libraries) and later plugins that we will require to make it work with PhoneGap. Perform the following steps:
GIT_HOME with a value C:\Program Files\git. Add additional information to the existing PATH by appending: ;%GIT_HOME%\bin.git command in the newly opened command prompt.C:\Android\apache-ant-1.9.4.ANT_HOME with the value C:\Android\apache-ant-1.9.4. Update the existing Path variable PATH by appending ;%ANT_HOME%\bin.node command in the command prompt. It will open the node console, which you can close by pressing Ctrl + C twice.npm install -g phonegap
phonegap command at the command prompt; this should return details about PhoneGap.android avd

phonegap create hello com.example.hello HelloWorld cd hello
phonegap run android —emulator
Change the font size
Change margin width
Change background colour