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
-
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.
-
Name your project, check the box to allow Mobile Hub to administer resources for you and then choose Add.
- Android - Java
-
-
Choose Android as your platform and then choose Next.
-
Choose the Download Cloud Config and then choose Next.
The
awsconfiguration.jsonfile 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.
-
Add the backend service configuration file to your app.
In the Project Navigator, right-click your app's
resfolder, and then choose New > Directory. Typerawas the directory name and then choose OK.
From the location where configuration file,
awsconfiguration.json, was downloaded in a previous step, drag it into theres/rawfolder. 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.jsoninto 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
-
-
Choose Android as your platform and then choose Next.
-
Choose the Download Cloud Config and then choose Next.
The
awsconfiguration.jsonfile 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.
-
Add the backend service configuration file to your app.
In the Project Navigator, right-click your app's
resfolder, and then choose New > Directory. Typerawas the directory name and then choose OK.
From the location where configuration file,
awsconfiguration.json, was downloaded in a previous step, drag it into theres/rawfolder. 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.jsoninto 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
-
-
Pick iOS as your platform and choose Next.
-
Choose the Download Cloud Config and then choose Next.
The
awsconfiguration.jsonfile 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.
-
Add the backend service configuration file to your app.
From your download location, place
awsconfiguration.jsoninto the folder containing yourinfo.plistfile 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.jsoninto 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 - Java
-
-
Prerequisites
-
Install Android Studio version 2.33 or higher.
-
Install Android SDK v7.11 (Nougat), API level 25.
-
-
Your
AndroidManifest.xmlmust contain:<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> -
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 } } -
Add the following code to the
onCreatemethod of your main or startup activity.AWSMobileClientis a singleton that establishes your connection to AWS 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
AWSMobileClientis initialized, it constructs theAWSCredentialsProviderandAWSConfigurationobjects 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, theonCompletemethod of theAWSStartUpHandleris 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
AWSCredentialsProviderandAWSConfigurationobjects which can be used to instantiate other SDK clients. You can use theIdentityManagerto 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
-
-
Prerequisites
-
Install Android Studio version 2.33 or higher.
-
Install Android SDK v7.11 (Nougat), API level 25.
-
-
Your
AndroidManifest.xmlmust contain:<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> -
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 } } -
Add the following code to the
onCreatemethod of your main or startup activity.AWSMobileClientis a singleton that establishes your connection to AWS 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
AWSMobileClientis initialized, it constructs theAWSCredentialsProviderandAWSConfigurationobjects 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 theonCompletemethod of theAWSStartUpHandler) 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
AWSCredentialsProviderandAWSConfigurationobjects which can be used to instantiate other SDK clients. You can use theIdentityManagerto 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
-
-
Prerequisites
-
Install Xcode version 8.0 or later.
-
-
Install Cocoapods. From a terminal window run:
sudo gem install cocoapods -
Create
Podfile. From a terminal window, navigate to the directory that contains your project's.xcodeprojfile and run:pod init -
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 -
Install dependencies by runnng:
pod install --repo-updateIf 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. -
The command
pod installcreates 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.xcworkspaceto open your Xcode project from now on. -
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.
-
Replace the
return truestatement indidFinishLaunchingwith 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
AWSMobileClientis 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, theonCompletemethod of theAWSStartUpHandleris 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
AWSCoreand add the following code todidFinishLaunchingWithOptionsbefore you returnAWSMobileClient.import AWSCore //. . . AWSDDLog.add(AWSDDTTYLogger.sharedInstance) AWSDDLog.sharedInstance.logLevel = .infoOptional: The following example shows how to retrieve the reference to
AWSCredentialsProviderobject which can be used to instantiate other SDK clients. You can use theAWSIdentityManagerto 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 } } -


