image

CONTENTS

Chapter 1: Introducing Cloud-Based Mobile Apps

How to Build Mobile Apps in the Cloud

Two Big Ideas About the App Industry

Getting Started

Summary

Chapter 2: Mobilizing Your App

Building a Touch-Sensitive Drawing App

Using the Amazon Cloud

Summary

Chapter 3: Building Mobile Web Apps

What You Can Do with Mobile Web Apps

Introducing jQuery and jQuery Mobile

Building a To-Do List App

Summary

Chapter 4: Enhancing Your App

Using App Caching

Handling Touch Events

Embedding an Interactive Map

Providing an Application Icon and a Startup Screen

Summary

Chapter 5: Building Apps in the Cloud

Server-Side JavaScript

Cloud Analytics for Your To-Do List App

Summary

Chapter 6: Use the Cloud!

The Classic Cloud Architecture

Introducing Amazon SimpleDB

Putting the To-Do List App in the Cloud

Summary

Chapter 7: Enhancing the User Experience

Creating a Classic Tab Bar Interface

Enabling Mobile Audio and Video

Launching Apps from Your App

Summary

Chapter 8: Working with the Cloud

Storing Content in Amazon S3

Signing In with the Cloud

Building Large-Scale Apps

Summary

Chapter 9: Creating Hybrid Apps that Run Natively

Introducing Hybrid Apps

Building Hybrid Apps

Lifestream, a Photo-Blogging App

Summary

Chapter 10: Building a Photo-Blogging App

The Architecture of Lifestream

Building the Server

Completing the Lifestream App

Summary

Chapter 11: Working with Cloud Development Services

Getting to Know the Mobile App Development Platforms

Using the FeedHenry Platform

Using the Appcelerator Platform

Using the appMobi Platform

Summary

Chapter 12: Going Social!

Using the Twitter API

An App for Direct Messages

Summary

Chapter 13: App Stores

What You Need to Publish Your App

Building Your App for Release

Summary

Chapter 14: Selling Your App

Determining a Marketing Strategy

Choosing Tactics for Promoting Your App

Summary

Appendix: Exercise Solutions

Introduction

Advertisement

Download CD/DVD content

image

For Orla, Saorla, Lola and Ruadhán, all my love.

ABOUT THE AUTHOR

RICHARD RODGER is the founder of Chartaca.com, a mobile analytics company built on the PhoneGap and Node.js technologies. He has led the successful development of several large scale enterprise mobile services, particularly in the news and media industry. He was previously CTO of FeedHenry.com, a JavaScript-focused cloud hosting platform for mobile apps, also based on PhoneGap and Node.js. As a senior researcher at the Telecommunications Software & Services Group at the Waterford Institute of Technology, Ireland, he led the research team that built the core platform that became FeedHenry. Richard is also the founder of Ricebridge.com, a company providing Java components for high volume data processing.

Richard has been a long-time participant in the Java open source community, and more recently in the Node.js community, contributing several modules for database integration and systems monitoring.

Richard has degrees in Computer Science from the Waterford Institute of Technology and Mathematics and Philosophy from Trinity College Dublin, Ireland.

CREDITS

ACQUISITIONS EDITOR

Paul Reese

PROJECT EDITOR

Kelly Talbot

TECHNICAL EDITORS

Matthew Baxter-Reynolds

Dale Cruse

PRODUCTION EDITOR

Becca Anderson

COPY EDITORS

Kitty Wilson

Kim Cofer

EDITORIAL MANAGER

Mary Beth Wakefield

FREELANCER EDITORIAL MANAGER

Rosemarie Graham

ASSOCIATE DIRECTOR OF MARKETING

David Mayhew

MARKETING MANAGER

Ashley Zurcher

BUSINESS MANAGER

Amy Knies

PRODUCTION MANAGER

Tim Tate

VICE PRESIDENT AND EXECUTIVE GROUP PUBLISHER

Richard Swadley

VICE PRESIDENT AND EXECUTIVE PUBLISHER

Neil Edde

ASSOCIATE PUBLISHER

Jim Minatel

PROJECT COORDINATOR, COVER

Katie Crocker

PROOFREADER

Jen Larsen, Word One New York

INDEXER

Robert Swanson

COVER DESIGNER

LeAndra Young

COVER IMAGE

© AnastasiyaShanhina

ACKNOWLEDGMENTS

THERE IS AN ENORMOUS DIFFERENCE BETWEEN talking about technology and writing about it. It is surprisingly difficult to place your thoughts on paper in coherent form. I do not think anyone can ever claim to have a written a book by themselves. Behind every book is a broad and deep support structure, without which no author would ever make it to the finish line.

I am deeply grateful to my wife Orla, and my children, Saorla, Lola and Ruadhán. I missed you. You were always unconditionally there for me and gave me the strength to write this book. I am also very grateful to my parents, Hanish and Noreen, and Orla’s parents, Noel and Kay. Thank you for helping us keep things together. Lauren, my sister, thank you for your words of encouragement. You are always thinking of us. And thank you, Alaister and Conor, you always believed in me.

I was delighted, in the Summer of 2010, to receive a call from Paul Reese at Wiley, inviting me to think about writing a book on the convergence of mobile apps and cloud computing. I’ve built two companies on this idea, and it’s only getting started! Thank you, Paul, for that call and your patience as I juggled work and writing.

A very heartfelt thank you to Kelly Talbot, my editor and guide. It is an absolute pleasure to work with such a great professional. Thank you for getting me through my first book.

Thank you, too, to the other members of the team, Matthew Baxter-Reynolds and Dale Cruse, for your astute technical input, and Kitty Wilson for so much detailed work improving my prose. This book is far better for your efforts. All remaining errors in text and code are mine alone.

And thank you to all those in my professional life, colleagues and customers alike. You have helped me learn and grow. There would be nothing to write without you.

INTRODUCTION

YOU CAN BUILD HIGH-QUALITY MOBILE APPS using only HTML, CSS, and JavaScript. And you can use JavaScript to build the cloud services that support those apps. You don’t need to learn Objective-C or Java to deliver production-quality iPhone and Android mobile apps.

This book shows you how to use the web development skills you already have to start working in the mobile app industry. With the support of new open source technologies like PhoneGap and Node.js, you can join the new industry momentum behind HTML5. With premium brands like Facebook and The Financial Times rolling out HTML5 mobile apps, the time is ripe to enter a new market with high demand for your development skills.

The move to standardize on HTML5 is one industry wave. The other is the move towards cloud computing. Cloud computing is the way most software services will be delivered in the near future. This book will show you how to work with cloud hosting services like Amazon, use cloud databases like MongoDB, authenticate against social media sites like Twitter and Facebook, and build large-scale high-uptime cloud APIs with Node.js to support your mobile app in production.

I wrote this book to give you the practical skills and tools you need to accelerate into this industry wave. The focus is on complete, fully working code samples (with error-handling!) that you can adapt for your own projects. During the course of this book, you will build three complete mobile apps, each covering key topics in mobile app development and cloud computing. I also wrote this book to communicate my genuine joy at rediscovering JavaScript, the oft-maligned language of the web. After years under the yoke of Java, programming is fun again!

WHO THIS BOOK IS FOR

This book is for two very different constituencies. The first is front-end web developers. You know your HTML, CSS, and JavaScript, but you’d really like to do more on the server-side. You know your way around Ruby, PHP, or Perl, but your first love is building fantastic user interfaces and web sites. You want to get into mobile app development, but you wonder if the time invested in learning Objective-C and Java is really worth it. This book shows you how to leverage your existing skills, letting you get started with mobile app development right away. And you can finally move beyond the client-side with JavaScript!

The second constituency is server-side developers. You’ve been stuck coding in Java or C# for years, and you need a change. You’d like to try your hand at mobile development, and of course you’ve already designed the cloud service to go with your app. This book shows you how to build the user interface of your app using standard web languages and how to structure your JavaScript at the level you’re accustomed to with Java and C#. JavaScript may be a scripting language, but it’s deeper than you think. This book also shows you how use JavaScript on the server-side. By doing this, you gain twice: There’s no mental context switch between client and server coding, and you can use Node.js to build large-scale event-based servers. Your productivity will double (mine has!), and you’ll be ready to build the next killer cloud service.

This material in this book is cumulative. Think of it as an in-depth extended tutorial that guides you step-by-step from first principles to happy clients and production apps. Each chapter takes you up another level. Most chapters cover client- and server-side topics, embracing their interdependence. Feel free to skip sections that introduce topics, like jQuery, that you might already be familiar with. But make sure you read the code. Often, old technologies are used in new ways.

The exercises are not written to test your knowledge of the material presented. Most of them will ask you to do something new, highlight something you need to think about, or take you off on an interesting tangent. While solutions are given, think of them more as suggestions. Trust your own instincts.

WHAT THIS BOOK COVERS

The client-side app code in this book uses the jQuery and jQuery Mobile open-source JavaScript libraries. These libraries are used to build complex, interactive user interfaces that demonstrate HTML structure manipulation, event handling, and animation effects. The latest versions of these libraries are used. These libraries are designed to work together and provide a solid basis for client-side development. They are widely used and well-tested by the community, and you are probably already familiar with at least jQuery.

To build mobile apps, this book introduces you to the latest features of HTML5 and then takes you further by showing you how to use the open-source PhoneGap project from Nitobi.com. You can deliver very substantial apps using just the web browser on your mobile device. Quite a lot of time is spent exploring how much you can do with HTML5. Eventually, of course, you’ll need to build “real” apps that can be listed in the Apple App Store and Android Marketplace. This is where PhoneGap comes in. This ingenious project provides a way for you to run your web code inside a native container app, meaning you can still build everything using HTML, CSS, and JavaScript, but you can also access physical device features like the camera.

The server-side code uses the Node.js JavaScript application server. The 0.4 stable branch is used throughout the book. As Node.js is under an active initial development phase, it is likely that the 0.6 or higher stable branch may be available by the time you read this. The code in this book is conservative with respect to the Node.js API and sticks to common API use-cases that are unlikely to change. Feel free to use the latest stable version, but do check the Errata page at www.wrox.com and my blog (http://tech.richardrodger.com) in case there are any specific issues. The Node.js server was chosen as the underlying platform for this book because it is the primary example of the new generation of event-based cloud servers. And you can code in JavaScript!

For data storage, the examples in this book use “noSQL” systems. MongoDB is used extensively because it uses the JSON format natively, which is very convenient for JavaScript code. The examples were build against the 1.8 release family. An example is also given of the purely cloud-based SimpleDB service from Amazon. This service has a well-defined web API and feature set that is unlikely to change much over time. These schemaless databases show you how to free yourself from the restrictions of the traditional SQL databases.

As the complexity of the examples in this book increases, so does the need to structure your code more carefully. As the code bases get larger, you’ll be given techniques to meet this challenge. You’ll learn about the DRY principle (Don’t Repeat Yourself), tracer bullets, unit and acceptance testing, use-case analysis, object-dependency mapping, and data structure design. All of these techniques are introduced in the context of a particular piece of code so that you have something concrete to help you understand how to apply them.

HOW THIS BOOK IS STRUCTURED

Each chapter in this book builds on the previous one. You’ll get the most value out of the material if you read it in sequence. You’ll build three apps in this book, each over the course of a few chapters:

Each app is constructed piece by piece, just as you would code it yourself. At the end of each chapter, you’ll have a working version of the app. It may not be feature-complete, but it will be a working app with a consistent code base.

Some chapters will take a detour to explore a key topic at a deeper level. You can skim this material if you just need to get your app built, returning later when you need some guidance on a particular topic.

Here are the topics covered by each chapter:

WHAT YOU NEED TO USE THIS BOOK

This book shows you how to build iPhone and Android mobile apps. You’ll need Mac OS X to build both iPhone and Android apps. On Windows or Linux, you’ll only be able to build Android apps. All the code in this book has been tested on both iPhone and Android, so you don’t absolutely need a Mac, but it is highly recommended.

You will definitely need either an iPhone or Android mobile device (preferably both!). Mobile apps always need to tested on physical devices to be absolutely sure of how they will behave in the real world.

For the cloud computing material, you’ll need to subscribe to various cloud services, like Amazon Web Services or MongoHQ.com. These are commercial services, and they generally require credit card details even if you do not intend to use the paid options. It is more than likely that you will end up paying some level of monthly charges if you begin to use these services seriously. That is in the nature of cloud services. Make sure you are ultimately making money from your clients to cover this!

CONVENTIONS

To help you get the most from the text and keep track of what’s happening, we’ve used a number of conventions throughout the book.

image

Boxes with a warning icon like this one hold important, not-to-be-forgotten information that is directly relevant to the surrounding text.

image

The pencil icon indicates notes, tips, hints, tricks, or asides to the current discussion.

As for styles in the text:

We present code in two different ways:

We use a monofont type with no highlighting for most code examples.
We use bold to emphasize code that is particularly important in the present context or to show changes from a previous code snippet.

SOURCE CODE

As you work through the examples in this book, you may choose either to type in all the code manually, or to use the source code files that accompany the book. All the source code used in this book is available for download at www.wrox.com. When at the site, simply locate the book’s title (use the Search box or one of the title lists) and click the Download Code link on the book’s detail page to obtain all the source code for the book. Code that is included on the website is highlighted by the following icon:

image

Listings include the filename in the title. If it is just a code snippet, you’ll find the filename in a code note such as this:

code snippet filename

image

Because many books have similar titles, you may find it easiest to search by ISBN; this book’s ISBN is 978-1-118-03469-9.

Once you download the code, just decompress it with your favorite compression tool. Alternately, you can go to the main Wrox code download page at www.wrox.com/dynamic/books/download.aspx to see the code available for this book and all other Wrox books.

ERRATA

We make every effort to ensure that there are no errors in the text or in the code. However, no one is perfect, and mistakes do occur. If you find an error in one of our books, like a spelling mistake or faulty piece of code, we would be very grateful for your feedback. By sending in errata, you may save another reader hours of frustration, and at the same time, you will be helping us provide even higher quality information.

To find the errata page for this book, go to www.wrox.com and locate the title using the Search box or one of the title lists. Then, on the book details page, click the Book Errata link. On this page, you can view all errata that has been submitted for this book and posted by Wrox editors. A complete book list, including links to each book’s errata, is also available at www.wrox.com/misc-pages/booklist.shtml.

If you don’t spot “your” error on the Book Errata page, go to www.wrox.com/contact/techsupport.shtml and complete the form there to send us the error you have found. We’ll check the information and, if appropriate, post a message to the book’s errata page and fix the problem in subsequent editions of the book.

P2P.WROX.COM

For author and peer discussion, join the P2P forums at p2p.wrox.com. The forums are a Web-based system for you to post messages relating to Wrox books and related technologies and interact with other readers and technology users. The forums offer a subscription feature to e-mail you topics of interest of your choosing when new posts are made to the forums. Wrox authors, editors, other industry experts, and your fellow readers are present on these forums.

At p2p.wrox.com, you will find a number of different forums that will help you, not only as you read this book, but also as you develop your own applications. To join the forums, just follow these steps:

1. Go to p2p.wrox.com and click the Register link.

2. Read the terms of use and click Agree.

3. Complete the required information to join, as well as any optional information you wish to provide, and click Submit.

4. You will receive an e-mail with information describing how to verify your account and complete the joining process.

image

You can read messages in the forums without joining P2P, but in order to post your own messages, you must join.

Once you join, you can post new messages and respond to messages other users post. You can read messages at any time on the Web. If you would like to have new messages from a particular forum e-mailed to you, click the Subscribe to this Forum icon by the forum name in the forum listing.

For more information about how to use the Wrox P2P, be sure to read the P2P FAQs for answers to questions about how the forum software works, as well as many common questions specific to P2P and Wrox books. To read the FAQs, click the FAQ link on any P2P page.