AWS Mobile
Developer Guide

iOS: Sync Data with Amazon Cognito Sync

New User?

Use AWS AppSync instead. AppSync is a new service for synchronizing application data across devices. Like Cognito Sync, AppSync enables synchronization of a user's own data, such as game state or app preferences. AppSync extends these capabilities by allowing multiple users to synchronize and collaborate in real-time on shared data, such as a virtual meeting space or chatroom.

Start building an iOS app with AWS AppSync now.

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 Setup for iOS.


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:

import AWSCore import AWSCognito
#import <AWSCore/AWSCore.h> #import <AWSCognito/AWSCognito.h>

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

let credentialProvider = AWSCognitoCredentialsProvider(regionType: .USEast1, identityPoolId: "YourIdentityPoolId") let configuration = AWSServiceConfiguration(region: .USEast1, credentialsProvider: credentialProvider) AWSServiceManager.default().defaultServiceConfiguration = configuration
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 Setup 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:

let dataset = AWSCognito.default().openOrCreateDataset("user_data")
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:

dataset?.setString("John Doe", forKey:"Username") dataset?.setString("10000", forKey:"HighScore")
[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:

_ = dataset?.synchronize()
[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.