AWS Mobile Hub Developer Guide
Developer Guide

iOS: Amazon Machine Learning

The following reference content only applies to existing apps that were built using the AWS Mobile SDKs for iOS and Android. If you’re building a new mobile or web app, or you're adding cloud capabilities to an existing app, visit the Amplify Framework website instead. Documentation for the AWS Mobile SDKs for iOS and Android is now part of the Amplify Framework.

Amazon Machine Learning (ML) is a service that makes it easy for developers of all skill levels to use machine learning technology. The SDK for iOS provides a simple, high-level client designed to help you interface with Amazon Machine Learning service. The client enables you to call Amazon ML's real-time API to retrieve predictions from your models and enables you to build mobile applications that request and take actions on predictions. The client also enables you to retrieve the real-time prediction endpoint URLs for your ML models.

Integrate Amazon Machine Learning

To use the Amazon Machine Learning mobile client, you’ll need to integrate the SDK for iOS into your app and import the necessary libraries. To do so, follow these steps:

  1. Download the SDK and unzip it as described in Setup the SDK for iOS.

  2. The instructions direct you to import the headers for the services you’ll be using. For Amazon Machine Learning, you need the following import.

iOS - SwiftiOS - Objective C
iOS - Swift
import AWSMachineLearning
iOS - Objective C
#import <AWSMachineLearning/AWSMachineLearning.h>

Configure Credentials

You can use Amazon Cognito to provide temporary AWS credentials to your application. These credentials let the app access your AWS resources. To create a credentials provider, follow the instructions at Providing AWS Credentials.

To use Amazon Machine Learning in an application, you must set the proper permissions. The following IAM policy allows the user to perform the actions shown in this tutorial on two actions identified by ARN.

{ "Statement": [{ "Effect": "Allow", "Action": [ "machinelearning:GetMLModel", "machinelearning:Predict" ], "Resource": "arn:aws:machinelearning:use-east-1:11122233444:mlmodel/example-model-id" }] }

This policy should be applied to roles assigned to the Amazon Cognito identity pool, but you need to replace the Resource value with the correct account ID and ML Model ID. You can apply policies at the IAM console. To learn more about IAM policies, see Introduction to IAM.

Create an Amazon Machine Learning Client

Once you've imported the necessary libraries and have your credentials object, you can instantiate AWSMachineLearningGetMLModelInput.

iOS - SwiftObjective C
iOS - Swift
let getMlModelInput = AWSMachineLearningGetMLModelInput()
Objective C
AWSMachineLearningGetMLModelInput *getMLModelInput = [AWSMachineLearningGetMLModelInput new];

Making a Predict Request

Prior to calling Predict, make sure you have not only a completed ML Model ID but also a created real-time endpoint for that ML Model ID. This cannot be done through the mobile SDK; you have to use the Machine Learning Console or an alternate SDK. To validate that this ML can be used for real-time predictions.

iOS - SwiftObjective C
iOS - Swift
// Use a created model that has a created real-time endpoint let mlModelId = "example-model-id"; // Call GetMLModel to get the realtime endpoint URL let getMlModelInput = AWSMachineLearningGetMLModelInput() getMlModelInput!.mlModelId = mlModelId; machineLearning.getMLModel(getMlModelInput!).continueOnSuccessWith { (task) -> Any? in if let getMLModelOutput = task.result { if (getMLModelOutput.status != AWSMachineLearningEntityStatus.completed) { print("ML Model is not completed"); return nil; } // Validate that the realtime endpoint is ready if (getMLModelOutput.endpointInfo!.endpointStatus != AWSMachineLearningRealtimeEndpointStatus.ready) { print("Realtime endpoint is not ready"); return nil; } } return nil }
Objective C
AWSMachineLearning *MachineLearning = [AWSMachineLearning defaultMachineLearning]; // Use a created model that has a created real-time endpoint NSString *MLModelId = @"example-model-id"; // Call GetMLModel to get the realtime endpoint URL AWSMachineLearningGetMLModelInput *getMLModelInput = [AWSMachineLearningGetMLModelInput new]; getMLModelInput.MLModelId = MLModelId; [[[MachineLearning getMLModel:getMLModelInput] continueWithSuccessBlock:^id(AWSTask *task) { AWSMachineLearningGetMLModelOutput *getMLModelOutput = task.result; // Validate that the ML model is completed if (getMLModelOutput.status != AWSMachineLearningEntityStatusCompleted) { NSLog(@"ML Model is not completed"); return nil; } // Validate that the realtime endpoint is ready if (getMLModelOutput.endpointInfo.endpointStatus != AWSMachineLearningRealtimeEndpointStatusReady) { NSLog(@"Realtime endpoint is not ready"); return nil; } }

Once the real-time endpoint is ready, we can begin calling Predict. Note that you must pass the real-time endpoint through the PredictRequest.

iOS - SwiftObjective C
iOS - Swift
// Create a Predict request with your ML Model id and the appropriate let predictInput = AWSMachineLearningPredictInput() predictInput!.predictEndpoint = getMLModelOutput.endpointInfo!.endpointUrl; predictInput!.mlModelId = mlModelId; predictInput!.record = record return machineLearning.predict(predictInput!)
Objective C
// Create a Predict request with your ML Model id and the appropriate Record mapping. AWSMachineLearningPredictInput *predictInput = [AWSMachineLearningPredictInput new]; predictInput.predictEndpoint = getMLModelOutput.endpointInfo.endpointUrl; predictInput.MLModelId = MLModelId; predictInput.record = @{}; // Call and return prediction return [MachineLearning predict:predictInput];

Additional Resources