Version 1: 1 Mobile App Developer Guide Last updated: July 4, 2014 © Copyright 2000–2014 salesforce.com, inc. All rights reserved. Salesforce.com is a registered trademark of salesforce.com, inc., as are other names and marks. Other marks appearing herein may be trademarks of their respective owners. Table of Contents Table of Contents Preface................................................................................................................................................1 Salesforce Platform Mobile Services.........................................................................................................................................2 Mobile Services in Force.com........................................................................................................................................2 Salesforce Mobile SDK.................................................................................................................................................3 Identity..........................................................................................................................................................................3 Salesforce1 Platform..................................................................................................................................................................3 When to Use Salesforce1 Platform vs. Creating a Custom App...............................................................................................4 About This Book......................................................................................................................................................................4 Version..........................................................................................................................................................................5 Sending Feedback..........................................................................................................................................................5 Chapter 1: Introduction to Mobile Development...................................................................................6 About Native, HTML5, and Hybrid Development.................................................................................................................7 Multi-Device Strategy...............................................................................................................................................................9 Developer Edition or Sandbox Environment?........................................................................................................................12 Development Prerequisites......................................................................................................................................................13 Sign Up for Force.com................................................................................................................................................14 Supported Browsers.................................................................................................................................................................14 Enough Talk; I’m Ready.........................................................................................................................................................16 Chapter 2: Getting Started..................................................................................................................17 Creating a Connected App.....................................................................................................................................................18 Create a Connected App.............................................................................................................................................18 Installing Mobile SDK............................................................................................................................................................20 Mobile SDK npm Packages........................................................................................................................................20 Do This First: Install Node.js and npm...........................................................................................................20 iOS Installation...............................................................................................................................................21 Android Installation........................................................................................................................................21 Uninstalling Mobile SDK npm Packages........................................................................................................22 Mobile SDK GitHub Repository................................................................................................................................23 Mobile SDK Sample Apps......................................................................................................................................................23 Installing the Sample Apps.........................................................................................................................................24 Installing Sample Apps for Android................................................................................................................24 Installing Sample Apps for iOS.......................................................................................................................25 What's New.............................................................................................................................................................................26 What's New in Mobile SDK 2.0.................................................................................................................................27 What's New in Mobile SDK 2.1.................................................................................................................................28 HTML5 Improvements in Visualforce (Winter ’14 Release)......................................................................................28 Chapter 3: HTML5 and Hybrid Development.....................................................................................30 Getting Started........................................................................................................................................................................31 Using HTML5 and JavaScript....................................................................................................................................31 HTML5 Development Requirements.........................................................................................................................31 i Table of Contents HTML5 Development Tools.................................................................................................................................................31 Mobile Design Templates...........................................................................................................................................31 HTML5 Mobile Templates Sample App.......................................................................................................32 Using Mobile Design Templates in Visualforce..............................................................................................33 Data Binding with Mobile Templates.............................................................................................................34 Using JavaScript Remoting to Query Contact Records...................................................................................34 Using Underscore to Generate the Template Markup....................................................................................35 Customizing Look and Feel............................................................................................................................36 List View Templates........................................................................................................................................37 Detail View Templates....................................................................................................................................45 Data Input Templates.....................................................................................................................................49 Map View Templates......................................................................................................................................56 Calendar View Templates...............................................................................................................................58 Report and Dashboard Templates...................................................................................................................61 Miscellaneous Templates.................................................................................................................................64 Mobile Packs...............................................................................................................................................................66 jQuery Quick Start..........................................................................................................................................67 Angular.js Quick Start.....................................................................................................................................68 Backbone.js Quick Start..................................................................................................................................70 Knockout Quick Start......................................................................................................................................71 Mobile UI Elements....................................................................................................................................................72 Using the Camera in HTML5: Mobile UI Elements Sample App.................................................................75 Delivering HTML5 Content With Visualforce......................................................................................................................80 Accessing Salesforce Data: Controllers vs. APIs.....................................................................................................................80 Introduction to Hybrid Development.....................................................................................................................................82 iOS Hybrid Development...........................................................................................................................................83 Android Hybrid Development....................................................................................................................................83 JavaScript Files for Hybrid Applications.....................................................................................................................83 Hybrid Apps Quick Start........................................................................................................................................................84 Running the Sample Hybrid App...............................................................................................................................85 How the Sample App Works..........................................................................................................................89 Create a Mobile Page to List Information..................................................................................................................90 Create a Mobile Page for Detailed Information..........................................................................................................93 Guidelines and Tips for Hybrid Apps.....................................................................................................................................95 Versioning and Javascript Library Compatibility........................................................................................................95 Example: Serving the Appropriate Javascript Libraries...............................................................................................97 Managing Sessions in Hybrid Applications................................................................................................................98 Chapter 4: Native iOS Development.................................................................................................101 iOS Native Quick Start.........................................................................................................................................................102 Native iOS Requirements.....................................................................................................................................................102 Creating an iOS Project........................................................................................................................................................102 Running the Xcode Project Template App...............................................................................................................104 Developing a Native iOS App...............................................................................................................................................104 About Login and Passcodes......................................................................................................................................105 ii Table of Contents About Memory Management....................................................................................................................................105 Overview of Application Flow...................................................................................................................................105 AppDelegate Class....................................................................................................................................................106 About View Controllers............................................................................................................................................107 RootViewController Class........................................................................................................................................108 About Salesforce REST APIs...................................................................................................................................109 Supported Operations....................................................................................................................................109 SFRestAPI Interface.....................................................................................................................................111 SFRestDelegate Protocol...............................................................................................................................111 Creating REST Requests..............................................................................................................................113 Sending a REST Request..............................................................................................................................113 SFRestRequest Class.....................................................................................................................................114 Using SFRestRequest Methods.....................................................................................................................114 SFRestAPI (Blocks) Category.......................................................................................................................115 SFRestAPI (QueryBuilder) Category............................................................................................................116 SFRestAPI (Files) Category..........................................................................................................................118 Tutorial: Creating a Native iOS Warehouse App.................................................................................................................119 Create a Native iOS App..........................................................................................................................................120 Step 1: Create a Connected App...................................................................................................................120 Step 2: Create a Native iOS Project..............................................................................................................121 Step 3: Run the New iOS App......................................................................................................................122 Step 4: Explore How the iOS App Works....................................................................................................123 Customize the List Screen.........................................................................................................................................125 Step 1: Modify the Root View Controller.....................................................................................................125 Step 2: Create the App's Root View .............................................................................................................126 Step 3:Try Out the App................................................................................................................................126 Create the Detail Screen............................................................................................................................................127 Step 1: Create the App's Detail View Controller..........................................................................................127 Step 2: Set Up DetailViewController............................................................................................................129 Step 3: Create the Designated Initializer.......................................................................................................131 Step 4: Establish Communication Between the View Controllers................................................................133 Step 5: Try Out the App...............................................................................................................................139 iOS Native Sample Applications...........................................................................................................................................139 Chapter 5: Native Android Development...........................................................................................140 Android Native Quick Start..................................................................................................................................................141 Native Android Requirements..............................................................................................................................................141 Creating an Android Project.................................................................................................................................................141 Setting Up Sample Projects in Eclipse..................................................................................................................................144 Android Project Files................................................................................................................................................144 Developing a Native Android App........................................................................................................................................145 The create_native Script............................................................................................................................................145 Android Application Structure..................................................................................................................................145 Native API Packages.................................................................................................................................................147 Overview of Native Classes.......................................................................................................................................148 iii Table of Contents SalesforceSDKManager Class.......................................................................................................................148 KeyInterface Interface....................................................................................................................................149 AccountWatcher Class..................................................................................................................................149 PasscodeManager Class.................................................................................................................................150 Encryptor class...............................................................................................................................................151 SalesforceActivity, SalesforceListActivity, and SalesforceExpandableListActivity Classes...........................151 UI Classes......................................................................................................................................................151 ClientManager Class.....................................................................................................................................151 RestClient Class............................................................................................................................................152 RestRequest Class..........................................................................................................................................152 FileRequests Class.........................................................................................................................................153 WrappedRestRequest Class...........................................................................................................................155 LoginActivity Class.......................................................................................................................................155 Other UI Classes...........................................................................................................................................155 UpgradeManager Class..................................................................................................................................155 Utility Classes................................................................................................................................................155 ForcePlugin Class..........................................................................................................................................156 Using Passcodes.........................................................................................................................................................156 Resource Handling....................................................................................................................................................157 Using REST APIs.....................................................................................................................................................159 Android Template App: Deep Dive..........................................................................................................................162 TemplateApp Class.......................................................................................................................................162 MainActivity Class........................................................................................................................................163 TemplateApp Manifest.................................................................................................................................164 Tutorial: Creating a Native Android Warehouse Application..............................................................................................164 Prerequisites..............................................................................................................................................................164 Create a Native Android App...................................................................................................................................166 Step 1: Create a Connected App...................................................................................................................166 Step 2: Create a Native Android Project.......................................................................................................166 Step 3: Run the New Android App...............................................................................................................167 Step 4: Explore How the Android App Works.............................................................................................167 Customize the List Screen.........................................................................................................................................168 Step 1: Remove Existing Controls.................................................................................................................168 Step 2: Update the SOQL Query..................................................................................................................169 Step 3:Try Out the App................................................................................................................................171 Create the Detail Screen............................................................................................................................................171 Step 1: Create the Detail Screen....................................................................................................................171 Step 2: Create the DetailActivity Class.........................................................................................................173 Step 3: Customize the DetailActivity Class...................................................................................................173 Step 4: Link the Two Activities, Part 1: Create a Data Class.......................................................................174 Step 5: Link the Two Activities, Part 2: Implement a List Item Click Handler...........................................175 Step 6: Implement the Update Button..........................................................................................................177 Step 7: Try Out the App...............................................................................................................................179 Android Native Sample Applications....................................................................................................................................179 iv Table of Contents Chapter 6: Files and Networking.......................................................................................................180 Architecture...........................................................................................................................................................................181 Downloading Files and Managing Sharing...........................................................................................................................181 Uploading Files.....................................................................................................................................................................181 Encryption and Caching.......................................................................................................................................................182 Using Files in Android Apps.................................................................................................................................................182 Managing the Request Queue...................................................................................................................................182 Using Files in iOS Native Apps............................................................................................................................................183 Managing Requests...................................................................................................................................................184 Using Files in Hybrid Apps...................................................................................................................................................185 Chapter 7: Offline Management.......................................................................................................186 Securely Storing Data Offline...............................................................................................................................................187 About SmartStore......................................................................................................................................................187 SmartStore Soups..........................................................................................................................................187 SmartStore Data Types.................................................................................................................................187 Enabling SmartStore in Hybrid Apps.......................................................................................................................188 Adding SmartStore to Existing Android Apps.........................................................................................................188 Registering a Soup.....................................................................................................................................................189 Retrieving Data From a Soup....................................................................................................................................190 Smart SQL Queries...................................................................................................................................................193 Working With Cursors.............................................................................................................................................194 Manipulating Data....................................................................................................................................................195 Using the Mock SmartStore......................................................................................................................................197 NativeSqlAggregator Sample App: Using SmartStore in Native Apps.....................................................................198 Using SmartSync to Access Salesforce Objects.....................................................................................................................200 About Backbone Technology....................................................................................................................................200 Models and Model Collections.................................................................................................................................201 Models...........................................................................................................................................................201 Model Collections.........................................................................................................................................202 Using the SmartSync Data Framework in JavaScript................................................................................................203 Offline Caching.........................................................................................................................................................204 Implementing Offline Caching.....................................................................................................................206 Using StoreCache For Offline Caching........................................................................................................207 Conflict Detection.....................................................................................................................................................210 Mini-Tutorial: Conflict Detection................................................................................................................212 Accessing Custom API Endpoints............................................................................................................................214 Force.RemoteObject Class............................................................................................................................214 Force.RemoteObjectCollection Class............................................................................................................214 Using Apex REST Resources........................................................................................................................216 Using External Objects (Beta).......................................................................................................................219 Tutorial: Creating a SmartSync Application.............................................................................................................220 Set Up Your Project.......................................................................................................................................220 Edit the Application HTML File.................................................................................................................221 v Table of Contents Create a SmartSync Model and a Collection.................................................................................................223 Create a Template.........................................................................................................................................224 Add the Search View.....................................................................................................................................225 Add the Search Result List View...................................................................................................................226 Add the Search Result List Item View..........................................................................................................227 Router............................................................................................................................................................228 SmartSync Sample Apps...........................................................................................................................................232 User and Group Search Sample.....................................................................................................................235 User Search Sample.......................................................................................................................................237 Account Editor Sample.................................................................................................................................240 Chapter 8: Push Notifications and Mobile SDK.................................................................................248 About Push Notifications......................................................................................................................................................249 Using Push Notifications in Android....................................................................................................................................249 Configure a Connected App For GCM (Android)...................................................................................................249 Code Modifications (Android)..................................................................................................................................250 Using Push Notifications in iOS...........................................................................................................................................251 Configure a Connected App for APNS (iOS)..........................................................................................................251 Code Modifications (iOS).........................................................................................................................................252 Chapter 9: Using Communities With Mobile SDK Apps....................................................................255 Communities and Mobile SDK Apps...................................................................................................................................256 Set Up an API-Enabled Profile............................................................................................................................................256 Set Up a Permission Set........................................................................................................................................................257 Grant API Access to Users...................................................................................................................................................258 Configure the Login Endpoint.............................................................................................................................................258 Branding Your Community..................................................................................................................................................259 Customizing Communities Login.........................................................................................................................................260 Using External Authentication With Communities.............................................................................................................262 About External Authentication Providers.................................................................................................................262 Using the Community URL Parameter....................................................................................................................263 Using the Scope Parameter........................................................................................................................................264 Configuring a Facebook Authentication Provider.....................................................................................................265 Configuring a Salesforce Authentication Provider....................................................................................................267 Configuring an OpenID Connect Authentication Provider.....................................................................................269 Example: Configure a Community For Mobile SDK App Access.......................................................................................271 Add Permissions to a Profile.....................................................................................................................................271 Create a Community.................................................................................................................................................272 Add the API User Profile To Your Community.......................................................................................................272 Create a New Contact and User................................................................................................................................272 Test Your New Community Login...........................................................................................................................273 Example: Configure a Community For Facebook Authentication.......................................................................................274 Create a Facebook App.............................................................................................................................................274 Define a Salesforce Auth. Provider...........................................................................................................................274 Configure Your Facebook App.................................................................................................................................275 vi Table of Contents Customize the Auth. Provider Apex Class................................................................................................................276 Configure Your Salesforce Community....................................................................................................................276 Chapter 10: Authentication, Security, and Identity in Mobile Apps....................................................278 OAuth Terminology.............................................................................................................................................................279 OAuth2 Authentication Flow...............................................................................................................................................279 OAuth 2.0 User-Agent Flow....................................................................................................................................280 OAuth 2.0 Refresh Token Flow................................................................................................................................281 Scope Parameter Values............................................................................................................................................281 Using Identity URLs.................................................................................................................................................282 Setting a Custom Login Server.................................................................................................................................287 Revoking OAuth Tokens..........................................................................................................................................288 Handling Refresh Token Revocation in Android Native Apps.................................................................................288 Token Revocation Events..............................................................................................................................288 Token Revocation: Passive Handling............................................................................................................289 Token Revocation: Active Handling.............................................................................................................289 Connected Apps....................................................................................................................................................................290 About PIN Security...................................................................................................................................................290 Portal Authentication Using OAuth 2.0 and Force.com Sites..............................................................................................291 Chapter 11: Distributing Mobile AppExchange Apps.........................................................................292 AppExchange for Mobile: Enterprise Mobile Apps.............................................................................................................293 Joining the AppExchange Partner Program..........................................................................................................................293 Get a Publishing Org............................................................................................................................................................294 Create a Provider Profile.......................................................................................................................................................294 The AppExchange Security Review......................................................................................................................................294 Index...............................................................................................................................................296 vii
Description: