Preface

As I posted the pain point in Cognito with Unauthenticated Identities in Javascript, I’d like to share how you achieve in iOS.

If you didn’t read previous post, I’d recommend you reading it first before you move on.

Overview

In this sample code, I’d like to invoke MOCK API Gateway with Cognito SDK in iOS and the service which would only response to client (mobile app) with signing requests.

Steps by Steps

Generate an SDK for an API with the API Gateway Console

Integrate an API Gateway-Generated iOS SDK into Your iOS Project

The generated SDK depends on the AWS Mobile SDK for iOS. There are two ways to import it into your project:

You should use one of these two ways to import the AWS Mobile SDK but not both. Importing both ways loads two copies of the SDK into the project and causes compiler errors.

With CocoaPods

With Frameworks

Use the SDK in your project

First import the AWSCore and the generated header files

#import <AWSCore/AWSCore.h>
#import "CLIMOCKClient.h"

To use AWS IAM to authorize API calls you should set an AWSCognitoCredentialsProvider object as the default provider for the SDK.

Replace [AWSRegionAPNortheast1] and [identity-pool-id] first

AWSCognitoCredentialsProvider *credentialsProvider = [[AWSCognitoCredentialsProvider alloc] 
        initWithRegionType:AWSRegionAPNortheast1    // Replace with the region you deploy
        identityPoolId:@"identity-pool-id"];
AWSServiceConfiguration *configuration = [[AWSServiceConfiguration alloc]
        initWithRegion:AWSRegionAPNortheast1 
        credentialsProvider:credentialsProvider];
AWSServiceManager.defaultServiceManager.defaultServiceConfiguration = configuration;

Get the identity id for this provider. If an identity id is already set on this provider, no remote call is made and the identity will be returned as a result of the AWSTask (the identityId is also available as a property). If no identityId is set on this provider, one will be retrieved from the service.

// Retrieve your Amazon Cognito ID
[[credentialsProvider getIdentityId] continueWithBlock:^id(AWSTask *task) {
    if (task.error) {
        NSLog(@"Error: %@", task.error);
    }
    else {
        // the task result will contain the identity id
        NSString *cognitoId = task.result;
    }
    return nil;
}];

Then grab the defaultClient from your code

CLIMOCKClient *client = [CLIMOCKClient defaultClient];

You can now call your method using the client SDK

[[client demoGet] continueWithBlock:^id(AWSTask *task) {
    if (task.error) {
        NSLog(@"Error: %@", task.error);
        return nil;
    }
    if (task.result) {
       CLIEmpty * output = task.result;
       //Do something with output
    }
    return nil;
}];

To use an API key with the API Gateway-generated SDK, you can set the apiKey property of the generated SDK to send API Keys in your requests. If you use an API key, it is specified as part of the x-api-key header and all requests to the API will be signed.

client.APIKey = @”api-key”;

References