Amazon Cognito Sync: Sync User Data

Authenticate Users with Amazon Cognito Identity#

Amazon Cognito Identity provides secure access to AWS services. Identities are managed by an identity pool. Roles specify resources an identity can access and are associated with an identity pool. To create an identity pool for your application:

  1. Log into the `Amazon Cognito Console`_ and click the New Identity Pool button
  2. Give your Identity Pool a unique name and enable access to unauthenticated identities
  3. Click the Create Pool button and then the Update Roles to create your identity pool and associated roles

For more information on Amazon Cognito Identity, see Amazon Cognito for iOS

Note

The auto-generated Roles include the permissions needed to access Amazon Cognito Sync, so no further configuration is required.

The next page displays code that creates a credential provider that provides a Amazon Cognito Identity for your app to use. Copy the code from Steps 1 & 2 into your AppDelegate.m file as shown below:

Add the following import statements:

Swift
import AWSCore
import AWSCognito
Objective-C
#import <AWSCore/AWSCore.h>
#import <AWSCognito/AWSCognito.h>

If you have an existing AWS credential provider, add the following code to application:didFinishLaunchingWithOptions method:

Swift
let credentialProvider = AWSCognitoCredentialsProvider(regionType: .USEast1, identityPoolId: "YourIdentityPoolId")
let configuration = AWSServiceConfiguration(region: .USEast1, credentialsProvider: credentialProvider)
AWSServiceManager.default().defaultServiceConfiguration = configuration
Objective-C
AWSCognitoCredentialsProvider *credentialsProvider = [[AWSCognitoCredentialsProvider alloc] initWithRegionType:AWSRegionUSEast1
identityPoolId:@"<your-identity-pool-arn>"];

AWSServiceConfiguration *configuration = [[AWSServiceConfiguration alloc] initWithRegion:AWSRegionUSEast1 credentialsProvider:credentialsProvider];

AWSServiceManager.defaultServiceManager.defaultServiceConfiguration = configuration;

For more information on Amazon Cognito Identity, see Amazon Cognito for iOS

Syncing User Data#

To sync unauthenticated user data:

  1. Create a dataset and add user data.
  2. Synchronize the dataset with the cloud.

Create a Dataset and Add User Data#

Create an instance of AWSCognitoDataset. User data is added in the form of key/value pairs. Dataset objects are created with the AWSCognito class which functions as a Amazon Cognito client object. Use the defaultCognito method to get a reference to the default singleton instance of AWSCognito. The openOrCreateDataset method is used to create a new dataset or open an existing instance of a dataset stored locally on the device:

Swift
let dataset = AWSCognito.default().openOrCreateDataset("user_data")
Objective-C
AWSCognitoDataset *dataset = [[AWSCognito defaultCognito] openOrCreateDataset:datasetName];:@"user_data"];

User data is added to an AWSCognitoDataset instance using the setString:forKey or setValue:forKey methods. The following code snippet shows how to add some user data to a dataset:

Swift
dataset?.setString("John Doe", forKey:"Username")
dataset?.setString("10000", forKey:"HighScore")
Objective-C
[dataset setString:@"John Doe" forKey:@"Username"];
[dataset setString:@"10000" forKey:@"HighScore"];

Synchronize Dataset with the Cloud#

To sync the dataset with the cloud, call the synchronize method on the dataset object:

Swift
_ = dataset?.synchronize()
Objective-C
[dataset synchronize];

All data written to datasets will be stored locally until the dataset is synced. The code in this section assumes you are using an unauthenticated Amazon Cognito identity, so when the user data is synced with the cloud it will be stored per device. The device has a device ID associated with it, when the user data is synced to the cloud, it will be associated with that device ID.

To sync user data across devices (based on an authenticated Cognito Identity) see Amazon Cognito Sync Developer Guide.