14 min read

 

iPhone JavaScript Cookbook

iPhone JavaScript Cookbook

Clear and practical recipes for building web applications using JavaScript and AJAX without having to learn Objective-C or Cocoa

        Read more about this book      

(For more resources related to this subject, see here.)

Introduction

Many web applications implement common features independent of the final purpose for which they have been designed. Functionalities and features such as authentication, forms validation, retrieving records from a database, caching, logging, and pagination are very common in modern web applications. As a developer, surely you have implemented one or more of these features in your applications more than once. Good developers and software engineers insist on concepts, such as modularity, reusability, and encapsulation; as a consequence you can find a lot of books, papers, and articles talking about how to design your software using these techniques. In fact, modern and popular methodologies, such as Extreme Programming, Scrum, and Test-driven Development are based on those principles. Although this approach sounds very appealing in theory, it might be complicated to carry it out in practice.

Developing any kind of software from scratch for running in any platform is undoubtedly a hard task. Complexity grows up when the target platform, operating system, or machine has its own specific rules and mechanisms. Some tools can make our job less complicated but only one kind of them is definitely a safe bet. It is here when we meet frameworks, a set of proven code that offers common functionality and standard structures for software development. This code makes our life much easier without reinventing the wheel and gives a skeleton to our applications, making sure that we’re doing things correctly. In addition, frameworks avoid starting from scratch once more. From a technical point of view, most frameworks are a set of libraries implementing functions, classes, and methods.

Using frameworks, we can save time and money, writing less code due to its code skeleton, and features implemented on it. Usually, frameworks force us to follow standards and they offer well-proven code avoiding common mistakes for beginners. Tasks such as testing, maintenance, and deployment are easier to do using frameworks due to the tools and mechanisms included. On the other hand, the learning curve could be a big and difficult drawback for beginners.

Through this article, we’ll learn how to install the main frameworks for JavaScript, HTML, and CSS development for iPhone. All of them offer a base to develop applications with a consistent and native look and feel using different methods. While some of them are focused on the user interface, others allow using AJAX in an efficient and easy way. Even some frameworks allow building native applications from the original code of the web application. We have the chance to choose which is better to fulfill our requirements; it is even possible to use more than one of these solutions for the same application. For our recipes, we’ll use the following frameworks:

  • iUI: This is focused on the look and feel of iPhone and consists of CSS files, images, and a small JavaScript library. Its objective is to get a web application running on the device with a consistent interface such as a native application. This framework establishes a correspondence between HTML tags and conventions used for developing native applications.
  • UiUIKit: Using a set of CSS and image files, it provides a coherent system for building web applications with a graphic interface such as native iPhone applications. The features offered for this framework are very similar to iUI.
  • XUI: This is a pure JavaScript library specific for mobile development. It has been designed to be faster and lighter than other similar libraries, such as jQuery, MooTools, and prototype.
  • iWebKit: This is developed specifically for Apple’s devices and is compatible with CSS3 standard; it helps to write web applications or websites with minimum HTML knowledge. Its modular design supports plugins for adding new features and we can build and use the themes for UI customization.
  • WebApp.Net: This framework comes loaded with JavaScript, CSS, and image files for developing web application for mobile devices that uses WebKit engine in its web browsers. Besides building interfaces, this framework includes functionality to use AJAX in an easy and efficient way.
  • PhoneGap: This is designed to minimize efforts for developing native mobile applications for different operating systems, platforms, and devices. It is based on the WORE (Write once, run anywhere) principle and it allows conversion from a web application into a native application. It supports many platforms and operating systems, such as iOS, Android, webOS, Symbian, and BlackBerry OS.
  • Apple Dashcode: Formally, this is a software development tool for Mac OS X included in Leopard and Snow Leopard versions, and focused on widget development for these operating systems. However, the last versions allow you to write web applications for iPhone and other iOS devices offering a graphic interface builder.

Installing the iUI framework

This recipe shows how to download and install the iUI framework on different operating systems. Particularly, we’ll cover Microsoft Windows, Mac OS X, and GNU/Linux.

Getting ready

The first step is to install and get ready; some tools need to be downloaded and decompressed. As computer users, we know how to decompress files using software such as WinZip, Ark, or the built-in utility on Mac OS X. You will surely have installed a web browser on your computer. If you are a Linux or Mac developer, you already know how to use curl or wget. These tools are very useful for quick download and you only need to use the command line through applications such as GNOME Terminal, Konsole, iTerm, or Terminal. iUI is an open source project, so you can download the code for free.

The open source project releases some stable versions packed and ready to download, but it is also possible to download a development version. This one could be suitable if you prefer working with the latest changes made by the official developers contributing to the project. Due to this, developers are using Mercurial version control and thus we’ll need to install a client for it to get access to this code.

How to do it…

iUI is an open source project so you can download the code for free. Open your favorite web browser and enter this URL:

http://code.google.com/p/iui/downloads/list

In that web page, you’ll see a list with files that refer to different release versions of this framework. Clicking on the link corresponding to the latest release’s drives takes you to a new web page that shows you a new link for the file. Click on it for instant downloading.

(Move the mouse over the image to enlarge it.)

If you are a GNU/Linux user or a Mac developer you will be used to command line. Open your terminal application and launch this command from your desired directory:

$ wget http://iui.googlecode.com/files/iui-0.31.tar.gz

Once you have downloaded the tarball file, it’s time to extract its content to a specific folder on our computer. WinZip and WinRAR are the most popular tools to do this task on Windows. Linux distributions, by default, install similar tools such as File Roller and Ark. Double-clicking from the download window of the Safari browser will extract the files directly to your default folder on your Mac, which is usually called Downloads. For command-line enthusiasts, execute the following command:

$ tar -zxvf iui-0.31.tar.gz

How it works…

After decompressing the downloaded file, you’ll find a folder with different subfolders and files. The most important is a subfolder called iui that contains CSS, images, and JavaScript files for building our web applications for iPhone. We need to copy this subfolder to our working folder where other application files reside.

Sharing this framework across different web applications is possible; you only need to put the iUI at a place where these applications have permissions to access. Usually, this place is a folder under the DocumentRoot of your web server. If you’re planning to write a high load application, it would be a good idea to use a cloud or CDN (Content Delivery Network) service such as Amazon Simple Storage Services (Amazon S3) for hosting and serving static HTML, CSS, JavaScript, and image files.

Installing the iUI framework is a straightforward process. You simply download and decompress one file, and then copy one folder into an other, which has permission to be accessed by the web server.

Apache is one of the most used and extended web servers in the world. Other popular options are Internet Information Server (IIS), lighttpd, and nginx. Apache web server is installed by default on Mac OS X; most of the operating systems based on Linux and UNIX offer binary packages for easy installation and you can find binary files for installing on Windows as well. IIS was designed for Windows operating systems, meanwhile, lighttpd and nginx are winning popularity and are used on UNIX systems as Linux’s distros, FreeBSD, and OpenBSD. Ubuntu Linux uses /var/www/ directory as the main DocumentRoot for Apache. So, in order to share iUI framework across applications, you can copy the folder to the other folder by executing this command:

$ cp -r iui-0.31/ui /var/www/iui

If you are a Mac user, your target directory will be /Library/WebServer/Documents/iui.

There’s more…

Inside the samples subfolder, you’ll find some files showing capabilities of this framework, including HTML and PHP files. Some examples need a web server with PHP support but you can test others using Safari web browser or an other WebKit’s browser such as Safari or Google Chrome. Open index.html with a web browser and use it as your starting point.

iPhone JavaScript: Installing Frameworks

If you prefer to use the latest version in development from the version control, you’ll need to install a Mercurial client. Most of the GNU/Linux distribution such as Fedora, Debian, and Ubuntu includes binary packages ready to install them. Usually, the name of the binary package is mercurial. The following command will install the client on Ubuntu Linux:

$ sudo apt-get install mercurial

Mercurial is an open source project and offers a binary file ready to install for Mac OS X and Windows systems. If you’re using one of these, go to the following page and download the specific file for your operating system and version:

http://mercurial.selenic.com/downloads/

After downloading, you can install the client using the regular process for your operating system. Mac users will find a ZIP file containing a binary package. For Windows, the distributed file is a MSI (Microsoft Installer), ready for self-installation after clicking on it.

Despite that the client of this version control was developed for the command line, we can find some GUI tools online such as TortoiseHG for Windows. These tools are intuitive and user-friendly, allowing an interactive use between the user and the source files hosted in the version control system. TortoiseHG can be downloaded from the same web page as the Mercurial client.

Finally, we’ll download the version development of the iUI framework executing the following command:

$ hg clone https://iui.googlecode.com/hg/ iui

The new iui folder includes all files of the iUI framework. We should copy this folder to our DocumentRoot.

If you want to know more about this framework, point your browser at the official wiki project:
http://code.google.com/p/iui/w/list
Also, taking a look at the complete code of the project may be interesting for advanced developers or just for people wanting to learn more about internal details:
http://code.google.com/p/iui/source/browse

Installing the UiUIKit framework

UiUIKit is the short name of the Universal iPhone UI Kit framework. The development of this framework is carried out through an open source project hosted in Google Code and is distributed under the GNU Public License v3. Let’s see how to install it on different operating systems.

Getting ready

As the main project file is distributed as a ZIP file, we’ll need to use one tool for decompressing these kind of files. Most of the modern operating systems include tools for this process. As seen in the previous recipe, we can use wget or curl programs for downloading the files.

If you are planning to read the source code or you’d like to use the current development version of the framework, you’ll need a Subversion client as the UiUIKit project is working with this open source version control.

How to do it…

Open your web browser and type the following URL:

http://code.google.com/p/iphone-universal/downloads/list

After downloading, click on the link for the latest version from the main list, for instance, the link called UiUIKit-2.1.zip. The next page will show you a different link for this file that represents the version 2.1 of the UiUIKit framework. Click on the link and the file will start downloading immediately.

Mac users will see how the Safari browser shows a window with the content of the compressed file, which is a folder called UiUIKit, which is stored in the default folder for downloads.

Command line’s fans can use these simple commands from their favorite terminal tool:

$ cd ~
$ curl -O http://iphone-universal.googlecode.com/files/
UiUIKit-2.1.zip

After downloading, don’t forget to decompress the file on your web-specific directory. The commands given next execute this action on Linux and Mac OS X systems:

$ cd /var/www/
$ unzip ~/UiUIKit-2.1.zip

How it works…

The main folder of the UiUIKit framework contains two subfolders called images and stylesheets. The first one includes many images used to get a native look for web applications on the iPhone. The other one offers a CSS file called iphone.css. We only need the images subfolder with its graphic files and the CSS file.

In order to use this framework in our projects, we need to allow our HTML files access to the images and the CSS file of the framework. These files should be in a folder with permissions for the web server. For example, we’ll have a directory structure for our new web application for iPhone as follows:

myapp/
index.html
images/
actionButtons.png
apple-touch-icon.png
backButton.png

toolButton.png
whiteButton.png
first.html
second.html
stylesheets/
iphone.css

Remember that this framework doesn’t include HTML files; we only need a bunch of the graphic files and one stylesheet file. The HTML files showed in the previous example will be our own files created for the web application.

We’ll also find a lot of examples on different HTML files located in the root directory, outside the mentioned subfolders. These files are not required for development but they can be very useful to show how to use some features and functionalities.

There’s more…

For an initial contact with the capabilities of the framework it would be interesting to take a look at the examples included in the main directory of the framework. We can load the index.html in our browser. This file is an index to the different examples and offers a native interface for the iPhone. Safari could be used but is better to access from a real iPhone device.

iPhone JavaScript: Installing Frameworks

Subversion is a well-proven version control used by many developers, companies, and, of course, open source projects. UiUIKit is an example of these projects using this popular version control. So, to access the latest version in development, we’ll need a client to download it. Popular Linux distributions, including Ubuntu and Debian have binary packages ready to install. For instance, the following command is enough to install it on Ubuntu Linux:

$ sudo apt-get install subversion

The last versions of Mac OS X, including Leopard and Snow Leopard, includes a Subversion client ready to use. For Windows, you can download Slik SVN available for 32-bit and 64-bits platforms; installation programs can be downloaded from:

http://www.sliksvn.com/en/download.

When you are sure that your client is running, you could execute it for getting the latest development version of the UiUIKit framework. Mac and Linux users will execute the following command:

$ svn checkout http://iphone-universal.googlecode.com/
svn/trunk/
UiUIKit

All information related to the UiUIKit framework project could be found at: http://code.google.com/p/iphone-universal/

LEAVE A REPLY

Please enter your comment!
Please enter your name here