Login with Amazon SDK for iOS 3.0.x Migration Guide
This guide explains how to migrate your app from using the Login with Amazon SDK for iOS v2.1.2 (or lower) to the Login with Amazon SDK for iOS v3.0.x. You will need to migrate from using APIs under the AIMobileLib
class to new APIs under AMZNAuthorizationManager
or AMZNUser
.
If you’ve not yet integrated Login with Amazon into your app, review the full set of instructions in our Getting Started Guide for iOS.
- How to Upgrade
- Handle the Login Button and Get User Profile Data
- Fetch User Profile Data
- Check for User Login at Startup
- Clear Authorization Data and Log Out a User
How to Upgrade
- Download the latest version of the Amazon Apps & Games Services SDK for iOS.
- Extract the files to a directory on your hard drive.
- Update libraries and frameworks, then build the project (for more detailed instructions, see Creating a Login with Amazon Project):
- If you used the Login with Amazon 1.0 or 2.0 library, delete the login-with-amazon-sdk directory and login-with-amazonsdk.a from the Frameworks folder. Click Edit from the main menu and select Delete. Also, remove any reference to old versions of LoginWithAmazon.framework if your project was using one.
- With your project open in Xcode, select the Frameworks folder, click File from the main menu, and then select Add files to
<project>
. In the dialog, select LoginWithAmazon.framework (v3.0.x) and click Add. - In the Build Phases section of your project, expand Link Binary with Libraries and click the plus sign to add the following frameworks to your project: Security.framework, SafariServices.framework, CoreGraphics.framework.
- Select Build Settings, then click All. Ensure the LoginWithAmazon.framework directory is in the Framework Search Paths. If you used the Login with Amazon 1.0 or 2.0 library, you can remove any references to the 1.0 or 2.0 library paths in the Header Search Paths or Library Search Paths.
- From the main menu, click Product and select Build. The build should complete successfully. Before building your project, if you used the Login with Amazon 1.0 or 2.0 library, replace
#import "AIMobileLib.h"
,#import "AIAuthenticationDelegate.h"
, or#import "AIError.h"
in your source files with#import <LoginWithAmazon/LoginWithAmazon.h>
.LoginWithAmazon.h
includes all of the Login with Amazon headers at once.
- Migrate to the new APIs introduced in the Login with Amazon 3.0 library as instructed below.
Handle the Login Button and Get User Profile Data
Call the authorize:withHandler method.
In the new LWA SDK for iOS, instead of calling authorizeUserForScopes:delegate:options:
, switch to call authorize:withHandler:
. To call this new API, you need to define an AMZNAuthorizeRequest
object. This request object allows you to customize input parameters to the authorize:withHandler:
API. Some properties commonly passed to the AMZNAuthorizeRequest
class are:
scopes
: Defines what scopes to request authorization for. TheAMZNProfileScope
class defines scopes provided by Login with Amazon. If you are using APIs for other Amazon products, you will find scopes supported by those products included in their own documentation.interactiveStrategy
: This is a newly-defined property that determines whether to prompt users to sign in whenauthorize:withHandler:
is called. The LWA SDK currently supports following strategies for prompting user sign in:AMZNInteractiveStrategyAuto
(default): The SDK looks for a locally stored authorization grant from previousauthorize:withHandler:
responses. If one is available, valid, and contains all requested scopes, the SDK will return a successful response viaAMZNAuthorizationRequestHandler
, and will not prompt the user to login. Otherwise, the user will be prompted to login.AMZNInteractiveStrategyAlways
: The SDK will always prompt the user to login regardless of whether they have previously been authorized to use the app. When the user is prompted, the SDK will remove all locally cached authorization grants for the app.AMZNInteractiveStrategyNever
: The SDK looks for a locally stored authorization grant from previousauthorize:withHandler
responses. If one is available, valid, and contains all requested scopes, the SDK will return anAMZNAuthorizeResult
object that contains an access token and user profile data. Otherwise, it will return anNSError
object viaAMZNAuthorizationRequestHandler
.
For a full list of properties in the AMZNAuthorizeRequest
object, see the class reference in the SDK documentation.
Add scopes to AMZNAuthorizeRequest.
In the new LWA SDK for iOS, we use the AMZNScope
object to represent a scope. To request scopes, you will need to add AMZNScope
objects to your AMZNAuthorizeRequest
. There are two options:
-
To request customer profile scopes provided by Login with Amazon, use the methods defined in the
AMZNProfileScope
class:Scope name Method in AMZNProfileScope class profile [AMZNProfileScope profile] postal_code [AMZNProfileScope postalCode] profile:user_id [AMZNProfileScope userID] -
Alternatively, you can create an
AMZNScope
object usingAMZNScopeFactory
:[AMZNScopeFactory scopeWithName:@"profile"]
Use this alternate method to request scopes provided by other Amazon products.
Use a block object to handle the callback.
Instead of using delegate methods, the new LWA SDK for iOS changes to use objective-c block objects to handle callback functions. This change eliminates the need to implement two delegate methods (one for requestDidSucceed:
and one for requestDidFail:
). Instead, implement a single AMZNAuthorizationRequestHandler
block object to process the result of the authorize:withHandler:
call. The AMZNAuthorizationRequestHandler
block contains three arguments:
-
result
: AnAMZNAuthorizeResult
object that contains the response from the Login with Amazon authorization server when theauthorize:withHandler:
call succeeds. Theresult
may include:token
: If you requested an access token (occurs by default), the LWA authorization server returns an access token in theresult
object. You no longer need to request tokens by making a separate call to thegetAccessTokenForScopes:withOverrideParams:delegate:
API in the success delegate method of theauthorizeUserForScopes:delegate:
API (a requirement in previous versions of the LWA SDK for iOS).user
: If you requested aprofile
scope, theresult
object contains anAMZNUser
object with the requested customer profile data. You no longer need to callgetProfile:
from the success delegate method ofauthorizeUserForScopes:delegate:
(a requirement in previous versions of the LWA SDK for iOS). See the class reference forAMZNUser
to find more information about obtaining profile data.
userDidCancel
: A boolean flag set to true if the customer chooses to cancel during the login flow.-
error
: AnNSError
object returned when internal errors occurred in the LWA SDK for iOS while processing theauthorize:withHandler:
request.- (IBAction)onLogInButtonClicked:(id)sender { // Build an authorize request. AMZNAuthorizeRequest *request = [[AMZNAuthorizeRequest alloc] init]; request.scopes = [NSArray arrayWithObjects: // [AMZNProfileScope userID], [AMZNProfileScope profile], [AMZNProfileScope postalCode]]; // Make an Authorize call to the Login with Amazon SDK. [[AMZNAuthorizationManager sharedManager] authorize:request withHandler:^(AMZNAuthorizeResult *result, BOOL userDidCancel, NSError *error) { if (error) { // Handle errors from the SDK or authorization server. } else if (userDidCancel) { // Handle errors caused when user cancels login. } else { // Authentication was successful. // Obtain the access token and user profile data. NSString *accessToken = result.token; AMZNUser *user = result.user; NSString *userID = user.userID; } }]; }
Fetch User Profile Data
As long as a user is logged in and authorized to your app, you can fetch their user profile data at any time. The new LWA iOS SDK for iOS introduces the AMZNUser
class to help you better manage customer profile data. Some of the commonly used customer profile data is defined as properties in this class:
userID
: the unique identifier of an customer.name
: the name of the customer.email
: the email address of the customer.postalCode
: the postal code of the customer.profileData
: A dictionary that contains all available profile data of the customer.
profile
scopes as described above.The new LWA SDK for iOS provides you two new options to request customer profile data, which replace calls to the getProfile:
API required by previous versions of the SDK:
- When the customer is not signed in to your app, call
authorize:withHandler:
to retrieve anAMZNUser
object in theresult
object of yourAMZNAuthorizationRequestHandler
block. -
If the customer is currently signed in to your app, call the
fetch:
API in theAMZNUser
class to get the most up-to-date customer profile data.[AMZNUser fetch:^(AMZNUser *user, NSError *error) { if (error) { // Error from the SDK, or no user has authorized to the app. } else if (user) { NSString *userID = user.userID; //NSString *name = user.name; //NSString *email = user.email; //NSString *postalCode = user.postalCode; // To get all available user profile data in a dictionary NSDictionary *profileData = user.profileData } }];
Check for User Login at Startup
In the new LWA SDK for iOS, you no longer need to call getAccessTokenForScopes:withOverrideParams:delegate:
. Instead, switch to call the authorize:withHandler:
API to detect whether your app is still authorized. Set AMZNAuthorizeRequest.interactiveStrategy
to AMZNInteractiveStrategyNever
and the SDK will look for a locally stored authorization grant from previous authorize:withHandler
responses. If one is available, valid, and contains all requested scopes, the SDK will return an AMZNAuthorizeResult
object that contains an access token and user profile data. Otherwise, it will return an NSError
object via AMZNAuthorizationRequestHandler
.
// Build an authorize request.
AMZNAuthorizeRequest *request = [[AMZNAuthorizeRequest alloc] init];
request.scopes = [NSArray arrayWithObjects:
// [AMZNProfileScope userID],
[AMZNProfileScope profile],
[AMZNProfileScope postalCode]];
request.interactiveStrategy = AMZNInteractiveStrategyNever;
[[AMZNAuthorizationManager sharedManager] authorize:request
withHandler:^(AMZNAuthorizeResult *result, BOOL
userDidCancel, NSError *error) {
if (error) {
// Error from the SDK, indicating the user was not previously authorized to your app for the requested scopes.
} else {
// The user was previously authorized to your app.
// Obtain the access token and user profile data.
NSString *accessToken = result.token;
AMZNUser *user = result.user;
NSString *userID = user.userID;
}
}];
Clear Authorization Data and Log Out a User
Use the new signOut:
API provided by the new LWA SDK for iOS, which replaces clearAuthorizationState:
.
[[AMZNAuthorizationManager sharedManager] signOut:^(NSError * _Nullable error) {
if (!error) {
// error from the SDK or Login with Amazon authorization server.
}
}];