Starting with AWS Cognito Sync


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:@”abhishek@knoldus.com” 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.

 

Reference:

https://aws.amazon.com/cognito/

http://docs.aws.amazon.com/cognito/latest/developerguide/cognito-sync.html

https://medium.com

 

This entry was posted in AWS, AWS Services and tagged , , , , , . Bookmark the permalink.

2 Responses to Starting with AWS Cognito Sync

  1. swethapriya says:

    Great article,i get new ideas because of all given information very excellent and real words to implemented for you.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s