Starting with AWS Cognito Sync

Table of contents
Reading Time: 2 minutes

Now a days people with multiple mobile device is growing. These people might use their phone on the go and their tablet when they at home. Consequently, they now want to be able to seamlessly transition from one device to another.cognitoimg
AWS Cognito Sync functionality to synchronise user data across an end user’s devices.
Imagine your app allows end user to add or bookmark link/data. Your end user probably get frustrated if they don’t get their important bookmarks over when they switch device.
To fix this you want to synchronise the bookmarks using cognito sync so they can resume where they left off.

Creating an Identity Pool :

First step is to create an identity pool. An identity pool holds the user identities.Each end user has his/her identity.

In order to sync data across devices, your identity pool must support authenticated identities like Facebook,Twitter,Amazon,Google+.

Unauthenticated identities are device specific,So unless an end user authenticates,no data can be synced across devices.

Syncing Data :
Once you have installed and configured your development environment, you need to instantiate the Cognito credentials provider in your app.

AWSCognitoCredentialsProvider credentialsProvider= [[AWSCognitoCredentialsProvider alloc] initWithRegionType:AWSRegionUSEast1 identityPoolId:@”<identity-Pool-Id>”];

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

AWSServiceManager.defaultServiceManager.defaultServiceConfiguration = configuration;

// Retrieve your Amazon Cognito ID.
NSString *cognitoIdString = credentialsProvider.identityId;
AWSTask *cognitoId = [credentialsProvider getIdentityId];
[AWSLogger defaultLogger].logLevel = AWSLogLevelVerbose;

User login with an identity provider and supply the Cognito credentials provider with the login token.

Now that you have a Cognito identity provider associated with the appropriate end users’ login, you can instantiate a sync client.
With a sync client, you can open a dataset and manipulate it.

AWSCognito *syncClient = [AWSCognito defaultCognito];

AWSCognitoDataset *dataset = [syncClient openOrCreateDataset:@”Dataset”];
[dataset setString:@”” forKey:@”Email”];
[dataset setString:@”Knoldus Software LLP” forKey:@”Company”];
[dataset setString:@”Software Consultant” forKey:@”Designation”];

We have created dataset.Now,How do we synchronize this data on another device? The process for accessing the data on another device is similar. Once you have opened your dataset and synchronized it, you can retrieve the value for a particular key like

[[dataset synchronize] continueWithBlock:^id(AWSTask *task) {
// Your handler code here
if (task.error) {
NSLog(@”Error ++++ : %@”, task.error);
else {
// The task result will contain the identity id
NSString *cognitoId = task.result;
BOOL cognitoId = task.completed;
//List all dataset in an Array
NSArray *alldataset = [[AWSCognito defaultCognito] listDatasets];
//Return all records of a dataset
NSArray *temp = [NSMutableArray arrayWithArray:[dataset getAllRecords]];
AWSCognitoRecord *record;
record recordId];
[dataset stringForKey:@”Email”];

return nil;

Because Cognito stores all data locally , you can continue to interact with your data even when you are offline.Anything you put into dataset, you can retrieve it anytime even if the device is offline.




Written by 

Joseph Ross is a Principal Consultant at Knoldus Inc. having more than 10 years of experience. Joseph has a passion for identifying challenges and give impactful solutions to the clients. He is a football fan and loves to watch TV series. Joseph has a cross-functional business operations and technology consulting experience. Joseph is familiar with programming languages such as Scala, C++, Java, CSS and HTML.

2 thoughts on “Starting with AWS Cognito Sync2 min read

Comments are closed.