26,99 €
Skip Objective-C and Java to get your app to market faster, using the skills you already have
Building Cross-Platform Apps using Titanium, Alloy, and Appcelerator Cloud Services shows you how to build cross-platform iOS and Android apps without learning Objective-C or Java. With detailed guidance given toward using the Titanium Mobile Platform and Appcelerator Cloud Services, you will quickly develop the skills to build real, native apps— not web apps—using existing HTML, CSS, and JavaScript know-how. This guide takes you step-by-step through the creation of a photo-sharing app that leverages the power of Appcelerator's cloud platform, and establishes fundamental concepts before adding advanced techniques. Coverage extends beyond the development process to include expert advice for deployment on the App Store or Google Play, and more.
The mobile app market is estimated at over $2.4 billion per year. These apps were traditionally built using Objective-C or Java, which can be complex and daunting to learn. Now you can use JavaScript on the Titanium framework to build amazing apps that run native on iOS and Android devices, and get your app to market faster with this guide.
When a great idea is in the works, no one wants to put it on hold to learn an entirely new skillset. Now there's an alternative. Get that app to market fast, using existing skills and powerful new tools, and grab a piece of that multi-billion-dollar market. Building Cross-Platform Apps using Titanium, Alloy, and Appcelerator Cloud Services is your ticket to the front of the line.
Sie lesen das E-Book in den Legimi-Apps auf:
Seitenzahl: 310
Veröffentlichungsjahr: 2014
This edition first published 2015
© 2015 Aaron Saunders
Registered office
John Wiley & Sons Ltd, The Atrium, Southern Gate, Chichester, West Sussex, PO19 8SQ, United Kingdom
For details of our global editorial offices, for customer services and for information about how to apply for permission to reuse the copyright material in this book please see our website at www.wiley.com.
The right of the author to be identified as the author of this work has been asserted in accordance with the Copyright, Designs and Patents Act 1988.
All rights reserved. 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 or otherwise, except as permitted by the UK Copyright, Designs and Patents Act 1988, without the prior permission of the publisher.
Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may not be available in electronic books.
Designations used by companies to distinguish their products are often claimed as trademarks. All brand names and product names used in this book are trade names, service marks, trademarks or registered trademarks of their respective owners. The publisher is not associated with any product or vendor mentioned in this book. This publication is designed to provide accurate and authoritative information in regard to the subject matter covered. It is sold on the understanding that the publisher is not engaged in rendering professional services. If professional advice or other expert assistance is required, the services of a competent professional should be sought.
Trademarks: Wiley and the Wiley logo are trademarks or registered trademarks of John Wiley & Sons, Inc. and/or its affiliates in the United States and/or other countries, and may not be used without written permission. All other trademarks are the property of their respective owners. John Wiley & Sons, Ltd. is not associated with any product or vendor mentioned in the book.
A catalogue record for this book is available from the British Library.
ISBN 978-1-118-67325-6 (paperback); ISBN 978-1-118-67324-9 (ePub); 978-1-118-67322-5 (ePDF)
Set in 10/12.5 ChaparralPro-Light by TCS/SPS
Printed in the United States by Bind-Rite
I dedicate this book to my father, Dennis Francis Saunders Sr., who supported me in my interest with computers way before computers where commonplace in society. He bought me my first computer—a Timex Sinclair—and I also remember the TRS-80 from Radio Shack. He passed away before the book could be finished, but he is the reason I became involved with computers.
Some of the people who helped bring this book to market include the following:
Editorial and Production
VP Consumer and Technology Publishing Director: Michelle Leete
Associate Director–Book Content Management: Martin Tribe
Associate Publisher: Chris Webb
Project Editor: Kezia Endsley
Copy Editor: Kezia Endsley
Technical Editor: Chaim Krause
Editorial Manager: Rev Mengle
Senior Project Editor: Sara Shlaer
Editorial Assistant: Claire Johnson
Marketing
Marketing Manager: Lorna Mein
Assistant Marketing Manager: Dave Allen
AARON SAUNDERS is the CEO/Founder of Clearly Innovative Inc., a minority-owned digital solutions provider headquartered in Washington DC with offices in New York City. The firm shapes ideas into viable products and transforms clients’ existing technologies into stunning solutions. Clearly Innovative is a leader in early adaption and implementation of cutting edge technologies, from mobile strategy and design to developing innovative web-based solutions. Clearly Innovative provides support and expertise through services focused on product strategy, user experience, design, and development.
Aaron is an experienced software developer with over 30 years of experience and has strong technical, communication, and collaboration abilities. He is highly adept at helping organizations add business value using mobile and web applications.
Aaron has a BA in Computer Science from Ohio Wesleyan University and an MBA with concentrations in Information Technology Strategy and Marketing from the NYU Stern School of Business.
This book would never have been started without the encouragement of Kwasi Frye to keep pressuring me to respond to requests to write a book.
This book would have never been completed without the patience and understanding of my wife Andrea Saunders who consistently gave me the time I needed to get this done, which was above and beyond the long hours of running a small digital agency, when I got home nights and sometime the whole weekend was spent writing code, reviewing chapters, and retesting the application for the book.
Thank you to Appcelerator for the platform you provided for me to start Clearly Innovative on, and thanks to all of the clients we worked with to develop mobile solutions and expand my knowledge of the Appcelerator platform and mobile application development.
Thanks to the team at Wiley who has been patient with me through the changes in the underlying Appcelerator platform that caused chapter rewrites, changes in the mobile user interface that required new screenshots, and delays in scheduling due to personal matters.
Thanks to Chaim Krause for being a great technical reviewer. I hope you learned something through the process.
Table of Contents
Introduction
Chapter 1: Installing and Configuring Appcelerator
Setting Up Titanium
Installing Titanium on the Mac
Installing Titanium Studio IDE
Installing Xcode
Installing the iOS Simulator
Installing the Titanium Command-Line Interface to Use an Alternate IDE
Installing the Android SDK
Installing Titanium Studio on Windows
Installing Titanium Studio
Installing Android SDK
Summary
Chapter 2: Introducing Appcelerator Cloud Services
Using the Appcelerator Cloud Services Console
Using Appcelerator Cloud Services REST API
Installing curl on a Device
Simple Test with the REST API
Integrating Appcelerator Cloud Services
Simple Example of Integrating Appcelerator Cloud Services
Summary
Chapter 3: Appcelerator Titanium Alloy Overview
Understanding the Model-View-Controller (MVC) Framework
Using Appcelerator Alloy with the MVC Framework
Backbone.js
Backbone.js in Alloy: Models and Collections
Using Sync Adapters
Basic Sync Adapter Construction
Backbone Model Events
Model-View Data Binding
Demo Project for Model View Binding
Creating the Model File
Creating the Collection Object
Data Binding with Models in Appcelerator Titanium Alloy
Updating the cars.js Controller File
Creating the New Controller/View for the Detail Display
Completing the Controller for the Detail View
Creating Widgets
Creating a More Complex Widget
Summary
Chapter 4: Building a Cross-Platform Social Photo-Sharing Application
Using Balsamiq to Design Mockups
Walking Through the Phone-Sharing App
User Accounts
Camera
Photo Uploading
Social Integration with Facebook
Finding Friends
Commenting and Rating of Media
Push Notifications
Application Flow
Summary
Chapter 5: Development Process for Cross-Platform Apps
Creating the Project for This Chapter
Preconfiguring Appcelerator Cloud Services
Creating the User Interface
Creating the Tab Group Files
Enabling the Camera Functionality on the Feed Tab
Adding a Custom Table Row to TableView
Integrating the Camera Functionality into the Application
Accessing the Device Camera in Appcelerator
Adding Camera API Calls to Feed Controller
Revisiting the FeedRow Controller
Revisiting the Feed Controller to Add the Rows to the Table
Adding Some Style to the Feed Table
Using the Android ActionBar for the Camera Button
Setting Up the index.xml View to Support the ActionBar
Modifying the index.xml View to Support the ActionBar
Adding the Alloy Sync Adapter and Appcelerator Cloud Services
Creating the User Model
Extending Alloy Models
Logging the User In
Creating Appcelerator Cloud Service Sync Adapter
Creating the Photo Model
Modifying the ACS Sync Adapter to Support the Photo Model
Model and Sync Adapter Working Together
Summary
Chapter 6: Integrating Comments
Creating the Comment Table View Layout
Rendering the Rows Using a Different View and Controller
Styling the Views to Match the Mockups
Adding Logic to the Controllers
Calling the New Controller from feed.js
Coding the comment.js Controller
Cross-Platform Support in Comment View
Coding the commentRow Controller
Adding Models and Collections for Querying Comments
Finishing the Comment Controllers
The commentRow Controller
Connecting the Dots . . . Showing the Comment List
Back to the feed and feedRow Controllers
Adding a New Comment to a Photo
Creating a New Comment Controller and View
Adding Code to the Comment Input Controller
Back to the Comment.js Controller
Saving the Comment and Updating the Table
Deleting Comments
Summary
Chapter 7: Integrating User Accounts with Appcelerator Cloud Services
Adding the Login User Interface
Updating the User Model
User Create Account Method
User Logout Method
Additional User Management Methods
Updating the Index Controller
Set Up the Basics in the Index Controller
Creating the Login Controller
Logging in the User
Creating the User Account
Using Facebook for Account Creation
Setting Up an Application to Use the Facebook Module
Facebook Button in the login.xml File
Facebook Method in the User Model
Facebook Handler in Login Controller
Updating User with Facebook Information
Check for Facebook Authentication on Startup
Logging Out of Facebook
Summary
Chapter 8: Working with Friends and Followers
Creating the CommonJS Library in Alloy
Adding the Code
Adding the Friends User Interface
Finishing Up the ListView with Style
Introduction to Appcelerator Cloud Services Friends Object
Modifying the ACS Sync Adapter to Support User Queries
Modifying the ACS Sync Adapter to Support Friends
Creating the Friend Relationship
Finding Friend Relationships Based on a User’s ID
Removing Friend Relationships from a User
Extending the User Model to Support User-Specific Friends Functionality
Integrating ListView Data-Binding with Friends Collections
Revisiting the friends.xml File
Integrating ListView Data-Binding with the Friends Controller
Displaying All Users
Displaying the Friends List
Working with User and Friends Lists
Removing a Friend from the Friends List
Updating the Application to Be Friend- and Location-Aware
Summary
Chapter 9: Working with Maps and Locations
Associating GPS Information When Saving a Photo
Modifying the Photo Model
Getting GPS Information from a Device
Creating a CommonJS Library for Geolocation
Updating the Feed Controller to Add Location to a Photo
Displaying the Photo Location on a Map
Android Support for Google Maps v2
Adding the Map Component to MapView XML
Displaying a Map of Photos Near Your Location
Querying ACS Photo Objects Using Your Current Location
Updating the User Interface to Show a Map View
Changes in the feed.js Controller
Responding to Clicks on Map Annotations
Summary
Chapter 10: Sharing via Facebook, Email, and Twitter
Creating the CommonJS Library for Sharing Functions
Facebook Permissions and Reauthorization
Sharing to the Facebook Wall
Sharing to the Facebook Album
Revisiting and Refactoring the Progress Window Library
Sharing to a Facebook Album
Sharing an Image as an Email Attachment
Twitter Integration with the social.js Module
Setting Up Your Twitter Developer Account
Adding social.js to Your Project
Adding the shareImage Function
Including the social.js Library in the Application
Adding Functionality to the sharing.js Library
Summary
Chapter 11: Push Notifications
Setting Up Push Notifications on Your Development Platform
Apple Push Notifications Configuration
Google Push Notifications Configuration
Configuring Push Notifications in Appcelerator Cloud Services
Creating the Push Notifications Library in an Application
Creating the pushNotifications.js Library
Getting the iOS Token
Getting the Android Token
Registering Callbacks
Integrating Push in Your Application
Registering for Push Notifications When the User Logs In
Sending Notifications Using the Appcelerator Cloud Services Console
Sending a Push Notification
Sending a Notification When Posting a Photo
Sending a Notification When Commenting on Photos
Sending a Notification When Adding a New Friend
Unregistering from Push Notifications When Logging Out
Further Integration of Push Notifications in Your Application
Summary
Chapter 12: Settings and User Management
Getting Started: View, Style, Controller
Editing the View
Editing the User Information in the Header Section
Editing the User Information Style
Handling Logout on Android and iOS
Logging the User Out
Logging Out of Appcelerator Push Notifications
Logging Out from Appcelerator Cloud Services
Logging Out from Social Media
Returning to the Login Screen
Setting the User’s Profile Picture
Adding a Few Performance Enhancements
Returning to the Feed Controller for Performance and UI Enhancement
Additional Information from the User Account
Adding Content to the Main View in the Settings Tab
Platform-Specific User Interface for Switch Control
Handling the Switch Initialization Values
Displaying Push Notification Status
Changing the Push Notification Status
Summary
Chapter 13: Going to Market: Deploying to the App Store and to Google Play
Process Overview
Registering for a Developer Account
Signing Your Application
Creating an App Record and Filling Out Metadata
Publishing Your Binary to the Store
iOS App Store Submission Process
Signing Up for an iOS Developer Account
Signing Your iOS Application
Creating an iTunes Connect Record
Publishing from Titanium Studio
Uploading Your Binary to the App Store
Google Play Submission Process
Signing Up for a Google Play Developer Account
Generating a Keystore for Publishing
Publishing to Google Play
Summary
Table of Contents
Begin Reading
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
349
350
351
352
353
354
355
356
357
358
359
360
361
362
THIS BOOK IS a high-level overview of using Appcelerator Titanium Alloy and Appcelerator Cloud Services to build native, cross-platform solutions in JavaScript. There is and will probably for a long time be an argument about the benefits of cross-platform solutions like Appcelerator, yet competitors continue to enter the field.
First and foremost, Appcelerator Titanium Alloy is not Phonegap; Appcelerator renders native user interfaces and provides a robust and extensible framework of APIs to interact with the native device on iOS, Android, Blackberry, and Windows Phone. Phonegap uses the mobile device’s web browser to render the user interface of the application and a collection of modules to interact with the device’s native capabilities. It is an acceptable solution for some, but I choose to focus on Appcelerator in my business, Clearly Innovative, because I believed, from a business and cost perspective, that we could provide the client with the native performance at a much better price point.
This book walks you through the process of building a photo-sharing application for the iOS and Android platforms on the Appcelerator Platform using the powerful new Alloy framework. This application integrates Appcelerator Platform’s MBaaS (mobile backend-as-a-service) and Appcelerator Cloud Services to create users, store photos, and implement push notifications in mobile applications.
The Appcelerator Community is very active through the Q&A forums, specific websites such as http://www.tidev.io/, and community-driven conferences like http://ticonf.org/. The Appcelerator company website documentation, training videos, and open sourced samples can provide you with additional supporting materials to help you get started in building your own great solution.
Building a great mobile solution is a fun yet sometimes challenging experience. It is my hope that this book makes the process a bit more enjoyable and manageable.
—Aaron K. Saunders
@aaronksaunders
https://github.com/aaronksaunders
Chapter 1
THIS CHAPTER BRIEFLY outlines the installation process for the Appcelerator tools. More detailed step-by-step instructions can be found on the Appcelerator site at http://docs.appcelerator.com/titanium/3.0/#%21/guide/Setting_up_Studio-section-37540095_SettingupStudio-installingstudio.
To install Titanium Studio, download the installer from the Appcelerator website. You will need to log in using your Appcelerator credentials, so register for an account if you haven’t done so already.
After launching Titanium Studio, you will need to configure native SDKs for each platform you want to support. Android works on both Mac and PC, but to develop for iOS you will need a Mac. See http://docs.appcelerator.com/titanium/3.0/#!/guide/Quick_Start for more information.
To install Titanium Studio on the Mac, download Titanium Studio from Appcelerator and install it. Then install Xcode and the Android SDK. The following sections cover this process in detail.
Figure 1-1: Downloading Titanium Studio from Appcelerator.
Figure 1-2: Dragging the Titanium Studio folder into the Applications folder.
Figure 1-3: Launching Titanium Studio.
Figure 1-4: Selecting a workspace.
After completing Step 5, your projects will be saved in Documents/Titanium_Studio_Workspace by default.
Figure 1-5: Logging in to use Titanium Studio.
Titanium Studio opens the Dashboard by default. You can reach the Dashboard again by clicking on the red home icon shown in Figure 1-6.
Figure 1-6: The Titanium Studio Dashboard is always accessible by clicking the red home icon.
Figure 1-7: Selecting iOS SDK from the Configure Native SDKs section.
Figure 1-8: Platform Configuration window (iOS).
Figure 1-9: Installing Xcode from the Mac App store.
Figure 1-10: Configuring of the Native SDK section is complete.
You will use the iOS Simulator regularly, so it’s important to install it next. Open Xcode and navigate to Xcode ⇒ Preferences ⇒ Downloads. Select each available version of the iOS Simulator, as shown in Figure 1-11. Click the Check and Install Now button.
Figure 1-11: Downloading the iOS Simulator in the Xcode Preferences section.
If you choose not to use the Titanium Studio IDE, you will need to set up Titanium on the command line. To do so, open Terminal and run the following two commands.
Node comes with Titanium Studio, so npm should work.
sudo npm install -g alloy
and
sudo npm install -g titanium
In the Titanium Studio Dashboard, select Android SDK and click Install or Update Android SDK. Then expand the Settings drop-down and select the Android API levels you want to support. Then click Configure. Note the Android SDK location: /Users/<username>/Library/android-sdk-macosx/. See Figure 1-12.
Figure 1-12: Installing Android SDKs in the platform configuration.
Library is a hidden folder, but you can reach it using Finder ⇒ Go and then holding down the Option key to reveal its location.
To install Titanium Studio on Windows, download Titanium Studio from Appcelerator. Then install the Android SDK (Xcode requires a Mac, so you will not be able to deploy to iOS using Windows). The following sections cover this process in detail.
Register for an account at www.appcelerator.com and then download Titanium Studio at www.appcelerator.com/titanium/download. See Figure 1-13.
Figure 1-13: Downloading Titanium Studio from Appcelerator.
Figure 1-14: Titanium Studio Setup wizard.
After completing Step 2, your projects will be saved in your Documents\Titanium_Studio_Workspace\ folder by default.
Figure 1-15: Selecting a workspace location.
Figure 1-16: Logging in to use Titanium Studio.
Dashboard opens by default and you can come back to it later by clicking on the red home icon.
Scroll to the bottom of Dashboard to the Configure Native SDKs section.Click Android SDK.Click the Install or Update Android SDK button, as shown in Figure 1-17.Figure 1-17: The Configure Native SDKs section.
Figure 1-18: Installing Android SDKs in the platforms you want to support.
Note the Android SDK default location of C:\android-sdk-win. You may need to reopen Titanium Studio to refresh the Android SDK status. Look for the green checkmark.
To set up your environment, download and install Titanium Studio. On the Get Started tab in the Dashboard, you can configure native SDKs. The native SDKs enable you to deploy your app to platforms such as Android and iOS. The Android SDK link allows you to download and install different Android SDK versions. And on Mac, the iOS SDK link opens the Xcode download page on the Mac App Store. Use the Dashboard to verify whether each SDK was installed properly. When you’re all set up properly, you’re ready to move on to Chapter 2, where you learn about all that Appcelerator Cloud Services has to offer.
Chapter 2
ONE OF THE bigger challenges in building a complex mobile application comes from an unexpected source, building the supporting backend systems for the application. The majority of applications out there interact with web services or databases to save or retrieve information for presentation in a mobile application. As a mobile developer, you need to find a solution since your goal is to develop mobile solutions, not build and maintain backend services and perform IT management and support.
The traditional approach is to build this backend system, find a place to host it, and then provide the appropriate resources to support it. Taking that approach in the mobile solutions world is cost-prohibitive, is an ongoing maintenance challenge, and is a financial burden on the mobile solution even before the application is launched.
Appcelerator Cloud Services provides a complete framework for integrating the backend services into your mobile application. These services are hosted and maintained by Appcelerator, the APIs are tested and supported by Appcelerator, and handling of the appropriate scaling as needed is also their responsibility. These Appcelerator-provided services enable you to create rich immersive mobile applications. You can extend the application’s services with the Node.ACS product and most importantly leverage the infrastructure for the backend provided by Appcelerator.
Key features of the Appcelerator Cloud Services are available out of the box. For example, comments, ratings, and reviews are supported through the API with no additional coding on the mobile developer’s side. Common location services such as check-ins and geo-querying are provided for all objects in the system. Photos and images are supported with built-in resizing, and blob storage comes ready to use. If the predefined objects do not meet your needs, you can save custom objects structured like JSON documents directly into the data store. Figure 2-1 shows all Appcelerator Cloud Services’ pre-built objects.
Figure 2-1: All of the Appcelerator Cloud Services’ pre-built objects.
An overview of Appcelerator Cloud Services can be found on the Appcelerator website at http://www.appcelerator.com/cloud/.
It is also important to know that even though you are using Appcelerator Cloud Services to integrate the Appcelerator Titanium mobile application, the framework provides a REST-based API, an iOS, and an Android native SDK that can be used for creating native applications.
To become more comfortable with Appcelerator Cloud Service, you are going to take a look at the Developer Console before you do any coding. You need to create an account at https://my.appcelerator.com/auth/signup. Figure 2-2 shows the sign-up page.
Figure 2-2: The Appcelerator developer’s sign-up page.
Follow these steps to create an account:
Enter your information and click the Sign Up button. You should be forwarded to the developer’s landing page. On the landing page is assorted information available to developers.Click the link entitled Create an ACS App from the section called “Getting Started with ACS” on the developer’s resources/landing page. Figure 2-3 shows this developer’s landing page.Figure 2-3: The Appcelerator developer’s landing page.
Figure 2-4: The Register A New App page.
If you created an app called wileyone, as the example here does, the resulting screen should look like Figure 2-5.
The left column provides basic overview information about your application and the right column shows the complete list of the predefined objects. You are going to create a user object first. Because most activities you will perform with Appcelerator Cloud Services require a user login, it’s best to get that out of the way first.
Click on the link titled Users(0) at the bottom of the app page. The (0) represents the number of existing objects of that type.Figure 2-5: The Appcelerator Cloud Services app page.
Figure 2-6 shows the screen displaying the results of the query for all user objects. Since you have not created any yet, the screen is empty. On the top left, notice the All Users and Admin Users tabs. Don’t worry about admin users, as they are covered later, when you create user accounts for testing the application.
Figure 2-6: The Appcelerator Cloud Services new user page.
Take a look at the input fields. You can see all the work and thought that went into defining the commonly used fields for users of the potential mobile application. If you need fields that are not included, you can extend the object using the custom_fields property, which is covered in more detail later, when you are extending objects in Appcelerator Cloud Services.
You can enter some basic data for the user in order to see how quickly the console gets going. Just enter an email address, username, and password. Figure 2-8 shows this page with some sample data.Figure 2-7: Creating a new user.
Figure 2-8:The New User Input page with sample data entered.
Figure 2-9: The All Users tab displaying the newly created user.
The console supports additional features such as deleting objects, exporting objects, and setting filters on the object display page. These features are just a few of the many features provided by the console. It is a great place to verify information when your application is not doing what you expect or to pre-populate some content to get the development process started.
Figure 2-10: The All Users tab expanded to show the newly created user’s field.
The Appcelerator Cloud Service has a REST API that allows you to create applications utilizing the framework as long as you have network capabilities. This means that Appcelerator Titanium apps and HTML5 applications utilizing AJAX clients both work.
Note the following from the Appcelerator Cloud Services documentation:
ACS is open to all app publishers, regardless of the development technology used to build the app-Titanium, Objective-C, Java, or HTML5 via frameworks like Sencha Touch or PhoneGap. Appcelerator Cloud Services provide a complete REST API along with iOS, Android, JavaScript, and ActionScript SDKs. Any device that can make HTTP requests over the Internet can securely use ACS as its server backend.
Because the purpose of the book is to demonstrate how to integrate Appcelerator Cloud Services with Appcelerator Titanium Alloy through a mobile application, it contains only a brief overview of the REST API. Additional information is available in the developer’s section of the Appcelerator website.
Although you can use the Appcelerator Cloud Services console in most cases, you might sometimes need quick access to content or want to quickly verify an API call. You can use the Mac OS terminal and the built-in curl command to access your Appcelerator Cloud Services content. On Windows machines, you can download the curl utility from http://curl.haxx.se/download.html. To access the content, you need the application key that was created in the previous section. The application key parameter is required on all REST API calls.
Two important points to note when using the REST API and the console:
You must always be logged in to access Appcelerator Cloud Services.You must save the session_id from the logged-in user to make Appcelerator Cloud Services REST API calls.If you log in to the Appcelerator developer site and take a look at the documentation for the REST API, you can see examples of how to use the API. You can also see the appropriate parameters for making the REST API call. The information regarding the URL format and the parameters are specified in the documentation. Figure 2-11 shows an example API reference documentation.
Figure 2-11: Sample of the Appcelerator Cloud Services API reference documentation.
You should now log in as the user you created in the previous section. The cookies.txt file will save the session information for use in other API calls. Type the information into the terminal, replacing the key parameter with your app ID.
$ curl -b cookies.txt -c cookies.txt -F "[email protected]" -F "password=wileyonetest" https://api.cloud.appcelerator.com/v1/users/login.json?key =[your-app-id]
The response should look something like the following code snippet, which indicates that you have successfully logged in to Appcelerator Cloud Services and the session information has been saved in the cookies.txt file. Please also note that all Appcelerator Cloud Services responses are in the JSON format. The meta section that follows always includes information about the specific query as well as a response section that lists the information on the objects that are returned from a successful request.
{ "meta": { "code": 200, "status": "ok", "method_name": "loginUser", "session_id": "Pn5a6z19prBWTiu2tI_MONtg7-M" }, "response": { "users": [ { "id": "51045756436d6921aa0a17e9", "created_at": "2013-01-26T22:23:18+0000", "updated_at": "2013-01-27T21:47:21+0000", "external_accounts": [ ], "confirmed_at": "2013-01-26T22:23:18+0000", "username": "[email protected]", "email": "[email protected]", "admin": "false", "stats": { "photos": { "total_count": 0 }, "storage": { "used": 0 } } } ] }
Now you’ll use the Appcelerator Cloud Services REST API to update the user object you created. If you recall, the user object has a first_name and a last_name field, which you did not set when the object was created. You can set those object properties on the command line using curl:
$ curl --verbose -b cookies.txt -c cookies.txt -X PUT --data-urlencode "first_name=Aaron" --data-urlencode "last_name=Saunders" "https://api.cloud.appcelerator.com/v1/users/ update.json? key=[your-app-id]"
You can get the value for your-app-id from the Appcelerator Cloud Services console on the Appcelerator website. After you log in to my.appcelerator.com/apps, select the appropriate application, and click Manage ACS. The new window will display a field titled “App Key,” which is the value to use for your-app-id.
The result shows the updated user object:
{ "meta": { "code": 200, "status": "ok", "method_name": "updateUser", "session_id": "Pn5a6z19prBWTiu2tI_MONtg7-M" }, "response": { "users": [ { "id": "51045756436d6921aa0a17e9", "first_name": "Aaron", "last_name": "Saunders", "created_at": "2013-01-26T22:23:18+0000", "updated_at": "2013-01-27T21:59:00+0000", "external_accounts": [ ], "confirmed_at": "2013-01-26T22:23:18+0000", "username": "[email protected]", "email": "[email protected]", "admin": "false", "stats": { "photos": { "total_count": 0 }, "storage": { "used": 0 } } } ] }
The same REST API used from the command line can be integrated into your Appcelerator Titanium mobile application, and in most cases the integration is done for you. For those who are interested, this section presents the same function performed previously using the REST API incorporated in a Titanium mobile application.
To connect to the service, you use the Appcelerator Titanium framework’s http client class, as well as the same appkey and API URL that you used on the command line.
This sample contains some advanced topics. Information about the Ti.Network.HTTPClient can be found in the Appcelerator Titanium documentation at
