Menu
AWS Mobile
Developer Guide

Get Started

Overview

The AWS Mobile Android and iOS SDKs help you build high quality mobile apps quickly and easily. They provide easy access to a range of AWS services, including Amazon Cognito, AWS Lambda, Amazon S3, Amazon Kinesis, Amazon DynamoDB, Amazon Pinpoint and many more.

Set Up Your Backend

  1. Sign up for the AWS Free Tier.

  2. Create a Mobile Hub project by signing into the console. The Mobile Hub console provides a single location for managing and monitoring your app's cloud resources.

    To integrate existing AWS resources using the SDK directly, without Mobile Hub, see Setup Options for Android or Setup Options for iOS.

  3. Name your project, check the box to allow Mobile Hub to administer resources for you and then choose Add.

Android - JavaAndroid - KotliniOS - Swift
Android - Java
  1. Choose Android as your platform and then choose Next.

  2. Choose the Download Cloud Config and then choose Next.

    The awsconfiguration.json file you download contains the configuration of backend resources that Mobile Hub enabled in your project. Analytics cloud services are enabled for your app by default.

  3. Add the backend service configuration file to your app.

    In the Project Navigator, right-click your app's res folder, and then choose New > Directory. Type raw as the directory name and then choose OK.

    
                           Image of creating a raw directory in Android Studio.

    From the location where configuration file, awsconfiguration.json, was downloaded in a previous step, drag it into the res/raw folder. Android gives a resource ID to any arbitrary file placed in this folder, making it easy to reference in the app.

    Remember

    Every time you create or update a feature in your Mobile Hub project, download and integrate a new version of your awsconfiguration.json into each app in the project that will use the update.

Your backend is now configured. Follow the next steps at Connect to Your Backend.

Android - Kotlin
  1. Choose Android as your platform and then choose Next.

  2. Choose the Download Cloud Config and then choose Next.

    The awsconfiguration.json file you download contains the configuration of backend resources that Mobile Hub enabled in your project. Analytics cloud services are enabled for your app by default.

  3. Add the backend service configuration file to your app.

    In the Project Navigator, right-click your app's res folder, and then choose New > Directory. Type raw as the directory name and then choose OK.

    
                           Image of creating a raw directory in Android Studio.

    From the location where configuration file, awsconfiguration.json, was downloaded in a previous step, drag it into the res/raw folder. Android gives a resource ID to any arbitrary file placed in this folder, making it easy to reference in the app.

    Remember

    Every time you create or update a feature in your Mobile Hub project, download and integrate a new version of your awsconfiguration.json into each app in the project that will use the update.

Your backend is now configured. Follow the next steps at Connect to Your Backend.

iOS - Swift
  1. Pick iOS as your platform and choose Next.

  2. Choose the Download Cloud Config and then choose Next.

    The awsconfiguration.json file you download contains the configuration of backend resources that Mobile Hub enabled in your project. Analytics cloud services are enabled for your app by default.

  3. Add the backend service configuration file to your app.

    From your download location, place awsconfiguration.json into the folder containing your info.plist file in your Xcode project. Select Copy items if needed and Create groups in the options dialog. Choose Next.

    Remember

    Every time you create or update a feature in your Mobile Hub project, download and integrate a new version of your awsconfiguration.json into each app in the project that will use the update.

Your backend is now configured. Follow the next steps at Connect to Your Backend.

Connect to Your Backend

Android - JavaAndroid - KotliniOS - Swift
Android - Java
  1. Prerequisites

  2. Your AndroidManifest.xml must contain:

    <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
  3. Add dependencies to your app/build.gradle, then choose Sync Now in the upper right of Android Studio. This libraries enable basic AWS functions, like credentials, and analytics.

    dependencies { implementation ('com.amazonaws:aws-android-sdk-mobile-client:2.6.+@aar') { transitive = true } }
  4. Add the following code to the onCreate method of your main or startup activity. AWSMobileClient is a singleton that establishes your connection to and acts as an interface for your services.

    import com.amazonaws.mobile.client.AWSMobileClient; public class YourMainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); AWSMobileClient.getInstance().initialize(this, new AWSStartupHandler() { @Override public void onComplete(AWSStartupResult awsStartupResult) { Log.d("YourMainActivity", "AWSMobileClient is instantiated and you are connected to AWS!"); } }).execute(); // More onCreate code ... } }

    What does this do?

    When AWSMobileClient is initialized, it constructs the AWSCredentialsProvider and AWSConfiguration objects which, in turn, are used when creating other SDK clients. The client then makes a Sigv4 signed network call to Amazon Cognito Federated Identities to retrieve AWS credentials that provide the user access to your backend resources. When the network interaction succeeds, the onComplete method of the AWSStartUpHandler is called.

Your app is now set up to interact with the AWS services you configured in your Mobile Hub project!

Choose the run icon () in Android Studio to build your app and run it on your device/emulator. Look for Welcome to AWS! in your Android Logcat output (choose View > Tool Windows > Logcat).

Optional: The following example shows how to retrieve the reference to AWSCredentialsProvider and AWSConfiguration objects which can be used to instantiate other SDK clients. You can use the IdentityManager to fetch the user's AWS identity id either directly from Amazon Cognito or from the locally cached identity id value.

import com.amazonaws.auth.AWSCredentialsProvider; import com.amazonaws.mobile.auth.core.IdentityHandler; import com.amazonaws.mobile.auth.core.IdentityManager; import com.amazonaws.mobile.client.AWSMobileClient; import com.amazonaws.mobile.client.AWSStartupHandler; import com.amazonaws.mobile.client.AWSStartupResult; import com.amazonaws.mobile.config.AWSConfiguration; public class YourMainActivity extends Activity { private AWSCredentialsProvider credentialsProvider; private AWSConfiguration configuration; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); AWSMobileClient.getInstance().initialize(this, new AWSStartupHandler() { @Override public void onComplete(AWSStartupResult awsStartupResult) { // Obtain the reference to the AWSCredentialsProvider and AWSConfiguration objects credentialsProvider = AWSMobileClient.getInstance().getCredentialsProvider(); configuration = AWSMobileClient.getInstance().getConfiguration(); // Use IdentityManager#getUserID to fetch the identity id. IdentityManager.getDefaultIdentityManager().getUserID(new IdentityHandler() { @Override public void onIdentityId(String identityId) { Log.d("YourMainActivity", "Identity ID = " + identityId); // Use IdentityManager#getCachedUserID to // fetch the locally cached identity id. final String cachedIdentityId = IdentityManager.getDefaultIdentityManager().getCachedUserID(); } @Override public void handleError(Exception exception) { Log.d("YourMainActivity", "Error in retrieving the identity" + exception); } }); } }).execute(); // .. more code } }
Android - Kotlin
  1. Prerequisites

  2. Your AndroidManifest.xml must contain:

    <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
  3. Add dependencies to your app/build.gradle, then choose Sync Now in the upper right of Android Studio. This libraries enable basic AWS functions, like credentials, and analytics.

    dependencies { implementation ('com.amazonaws:aws-android-sdk-mobile-client:2.6.+@aar') { transitive = true } }
  4. Add the following code to the onCreate method of your main or startup activity. AWSMobileClient is a singleton that establishes your connection to and acts as an interface for your services.

    import com.amazonaws.mobile.client.AWSMobileClient; class YourMainActivity : Activity() { companion object { private val TAG: String = this::class.java.simpleName } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState); AWSMobileClient.getInstance().initialize(this) { Log.d(TAG, "AWSMobileClient is initialized") }.execute() // More onCreate code... } }

    What does this do?

    When AWSMobileClient is initialized, it constructs the AWSCredentialsProvider and AWSConfiguration objects which, in turn, are used when creating other SDK clients. The client then makes a Sigv4 signed network call to Amazon Cognito Federated Identities to retrieve AWS credentials that provide the user access to your backend resources. When the network interaction succeeds, the callback (which is technically the onComplete method of the AWSStartUpHandler) is called.

Your app is now set up to interact with the AWS services you configured in your Mobile Hub project!

Choose the run icon () in Android Studio to build your app and run it on your device/emulator. Look for Welcome to AWS! in your Android Logcat output (choose View > Tool Windows > Logcat).

Optional: The following example shows how to retrieve the reference to AWSCredentialsProvider and AWSConfiguration objects which can be used to instantiate other SDK clients. You can use the IdentityManager to fetch the user's AWS identity id either directly from Amazon Cognito or from the locally cached identity id value.

import com.amazonaws.auth.AWSCredentialsProvider import com.amazonaws.mobile.auth.core.IdentityHandler import com.amazonaws.mobile.auth.core.IdentityManager import com.amazonaws.mobile.client.AWSMobileClient import com.amazonaws.mobile.config.AWSConfiguration class YourMainActivity : Activity() { companion object { private val TAG: String = this::class.java.simpleName } private var credentialsProvider: AWSCredentialsProvider? = null private var awsConfiguration: AWSConfiguration? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState); AWSMobileClient.getInstance().initialize(this) { credentialsProvider = AWSMobileClient.getInstance().credentialsProvider awsConfiguration = AWSMobileClient.getInstance().configuration IdentityManager.getDefaultIdentityManager().getUserID(object : IdentityHandler { override fun handleError(exception: Exception?) { Log.e(TAG, "Retrieving identity: ${exception.message}") } override fun onIdentityId(identityId: String?) { Log.d(TAG, "Identity = $identityId") val cachedIdentityId = IdentityManager.getDefaultIdentityManager().cachedUserID // Do something with the identity here } }) }.execute() // More onCreate code... } }
iOS - Swift
  1. Prerequisites

  2. Install Cocoapods. From a terminal window run:

    sudo gem install cocoapods
  3. Create Podfile. From a terminal window, navigate to the directory that contains your project's .xcodeproj file and run:

    pod init
  4. Add core AWS Mobile SDK components to your build.

    platform :ios, '9.0' target :'YOUR-APP-NAME' do use_frameworks! pod 'AWSMobileClient', '~> 2.6.13' # other pods end
  5. Install dependencies by runnng:

    pod install --repo-update

    If you encounter an error message that begins "[!] Failed to connect to GitHub to update the CocoaPods/Specs . . .", and your internet connectivity is working, you may need to update openssl and Ruby.

  6. The command pod install creates a new workspace file. Close your Xcode project and reopen it using ./YOUR-PROJECT-NAME.xcworkspace.

    Use ONLY your .xcworkspace

    Remember to always use ./YOUR-PROJECT-NAME.xcworkspace to open your Xcode project from now on.

  7. Rebuild your app after reopening it in the workspace to resolve APIs from new libraries called in your code. This is a good practice any time you add import statements.

  8. Replace the return true statement in didFinishLaunching with the following code in your AppDelegate to establish a run-time connection with AWS Mobile.

    import UIKit import AWSMobileClient @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. // Create AWSMobileClient to connect with AWS return AWSMobileClient.sharedInstance().interceptApplication( application, didFinishLaunchingWithOptions: launchOptions) }

    What does this do?

    When AWSMobileClient is initialized, it makes a Sigv4 signed network call to Amazon Cognito Federated Identities to retrieve AWS credentials that provide the user access to your backend resources. When the network interaction succeeds, the onComplete method of the AWSStartUpHandler is called.

Your app is now set up to interact with the AWS services you configured in your Mobile Hub project!

Choose the run icon () in the top left of the Xcode window or type -R to build and run your app. Look for Welcome to AWS! in the log output.

Optional: If you want to make sure you're connected to AWS, import AWSCore and add the following code to didFinishLaunchingWithOptions before you return AWSMobileClient.

import AWSCore //. . . AWSDDLog.add(AWSDDTTYLogger.sharedInstance) AWSDDLog.sharedInstance.logLevel = .info

Optional: The following example shows how to retrieve the reference to AWSCredentialsProvider object which can be used to instantiate other SDK clients. You can use the AWSIdentityManager to fetch the AWS identity id of the user from Amazon Cognito.

import UIKit import AWSMobileClient import AWSAuthCore class ViewController: UIViewController { @IBOutlet weak var textfield: UITextField! override func viewDidLoad() { super.viewDidLoad() textfield.text = "View Controller Loaded" // Get the AWSCredentialsProvider from the AWSMobileClient let credentialsProvider = AWSMobileClient.sharedInstance().getCredentialsProvider() // Get the identity Id from the AWSIdentityManager let identityId = AWSIdentityManager.default().identityId } }

Next Steps