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
Beginning Mobile Application Development in the Cloud
Published by
John Wiley & Sons, Inc.
10475 Crosspoint Boulevard
Indianapolis, IN 46256
www.wiley.com
Copyright © 2012 by John Wiley & Sons, Inc., Indianapolis, Indiana
Published simultaneously in Canada
ISBN: 978-1-118-03469-9
ISBN: 978-1-118-20333-0 (ebk)
ISBN: 978-1-118-20335-4 (ebk)
ISBN: 978-1-118-20334-7 (ebk)
Manufactured in the United States of America
10 9 8 7 6 5 4 3 2 1
No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600. Requests to the Publisher for permission should be addressed to the Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-6008, or online at http://www.wiley.com/go/permissions.
Limit of Liability/Disclaimer of Warranty: The publisher and the author make no representations or warranties with respect to the accuracy or completeness of the contents of this work and specifically disclaim all warranties, including without limitation warranties of fitness for a particular purpose. No warranty may be created or extended by sales or promotional materials. The advice and strategies contained herein may not be suitable for every situation. This work is sold with the understanding that the publisher is not engaged in rendering legal, accounting, or other professional services. If professional assistance is required, the services of a competent professional person should be sought. Neither the publisher nor the author shall be liable for damages arising herefrom. The fact that an organization or Web site is referred to in this work as a citation and/or a potential source of further information does not mean that the author or the publisher endorses the information the organization or Web site may provide or recommendations it may make. Further, readers should be aware that Internet Web sites listed in this work may have changed or disappeared between when this work was written and when it is read.
For general information on our other products and services please contact our Customer Care Department within the United States at (877) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002.
Wiley also publishes its books in a variety of electronic formats and by print-on-demand. Not all content that is available in standard print versions of this book may appear or be packaged in all book formats. If you have purchased a version of this book that did not include media that is referenced by or accompanies a standard print version, you may request this media by visiting http://booksupport.wiley.com. For more information about Wiley products, visit us at www.wiley.com.
Library of Congress Control Number: 2011936907
Trademarks: Wiley, the Wiley logo, Wrox, the Wrox logo, Programmer to Programmer, and related trade dress are trademarks or registered trademarks of John Wiley & Sons, Inc. and/or its affiliates, in the United States and other countries, and may not be used without written permission. All other trademarks are the property of their respective owners. John Wiley & Sons, Inc., is not associated with any product or vendor mentioned in this book.
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:
- A classic To Do List app
- A Photo-blogging app
- A Twitter app
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:
- Chapter 1, “Introducing Cloud-Based Mobile Apps,” gets you up to speed with HTML5 and cloud servers, showing you how to deliver a simple mobile web app.
- Chapter 2, “Mobilize Your App!,” starts to make your app behave more like a native app, and walks you through a deployment onto an Amazon cloud server.
- Chapter 3, “Building Mobile Web Apps,” introduces the jQuery Mobile framework, giving your app a truly native look and feel, and starts the development of the To Do List app.
- Chapter 4, “Enhance Your App!,” takes a closer look at touch and gesture events, and teaches you how to deal with them.
- Chapter 5, “Building Apps in the Cloud,” introduces the Node.js server and builds a scalable event tracking system for the To Do List app.
- Chapter 6, “Use the Cloud!,” converts the To Do List app to use the Amazon SimpleDB cloud database, showing you how to build on cloud services.
- Chapter 7, “Enhancing the User Experience,” dives deeper into the features you’ll need to build to create a native feel for you app, including momentum scrolling and audio-visual media playback.
- Chapter 8, “Working with the Cloud,” lays the foundation for the photo-blogging app by showing you how to interact with the Amazon S3 cloud storage service and how to use the OAuth protocol to let your users log in with Twitter and Facebook.
- Chapter 9, “Creating Hybrid Apps that Run Natively,” takes the photo-blogging app native, using PhoneGap, to create iPhone and Android apps built using HTML.
- Chapter 10, “Building a Photo-Blogging App” pulls together everything you’ve learned so far to deliver a complete photo-blogging app that can interact with the on-device camera and provide Twitter-like scalable follower/friend relationships for your users.
- Chapter 11, “Working with Cloud Services,” takes you on a tour of the commercial cloud services that can make your life much easier as an HTML5 mobile app developer.
- Chapter 12, “Go Social!,” builds a complete twitter client app that lets you manage your direct message conversations over multiple Twitter accounts.
- Chapter 13, “App Stores,” takes you step-by-step through the app submission process for the Apple App Store and Android Marketplace
- Chapter 14, “Selling Your App,” gives you a head start on your competitors by giving you some great ideas for promoting and selling your app.
- The Appendix, “Exercise Solutions,” provides answers to the exercises found at the end of 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.
Boxes with a warning icon like this one hold important, not-to-be-forgotten information that is directly relevant to the surrounding text.
The pencil icon indicates notes, tips, hints, tricks, or asides to the current discussion.
As for styles in the text:
- We highlight new terms and important words when we introduce them.
- We show keyboard strokes like this: Ctrl+A.
- We show file names, URLs, and code within the text like so: persistence.properties.
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:
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:
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.
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.