Ramiro Ramiro - 4 months ago 64
iOS Question

[iOS][AWS Cognito] 'logins' is deprecated: Use "AWSIdentityProviderManager"

I’ve been trying to authenticate user with Facebook and Twitter on iOS with Amazon Cognito.
I can’t implement because Official documents is old.

Here is my code:

NSString *token = [FBSDKAccessToken currentAccessToken].tokenString;

credentialsProvider = [[AWSCognitoCredentialsProvider alloc] initWithRegionType:AWSRegionAPNortheast1 identityPoolId:IDENTITY_POOL_ID];
AWSServiceConfiguration *configuration = [[AWSServiceConfiguration alloc] initWithRegion:AWSRegionAPNortheast1
credentialsProvider:credentialsProvider];

credentialsProvider.logins = @{ AWSIdentityProviderFacebook: token };
NSLog(@"credentialsProvider.logins : %@", credentialsProvider.logins);
[AWSServiceManager defaultServiceManager].defaultServiceConfiguration = configuration;


But Xcode says that
‘logins’ is deprecated: Use “AWSIdentityProviderManager” to provide a valid logins dictionary to the credentials provider


I figured out that credentialsProvider.logins returns [null] as logins is deprecated.

Amazon official documents (English, Japanese) & samples are not up-to-date so I don’t know how to implement correctly to authenticate user.

Finally, I found a solution for this in Swift but I don’t know.

AWS Cognito Swift credentials provider "logins is deprecated: Use AWSIdentityProviderManager"

import Foundation
import AWSCore
import AWSCognito
import AWSCognitoIdentityProvider
class CustomIdentityProvider: NSObject, AWSCognitoIdentityProviderManager{
var tokens : [NSString : NSString]?
init(tokens: [NSString : NSString]) {
self.tokens = tokens
}
@objc func logins() -> AWSTask {
return AWSTask(result: tokens)
}
}


let customProviderManager = CustomIdentityProvider(tokens: logins!)

self.credentialsProvider = AWSCognitoCredentialsProvider(
regionType: Constants.COGNITO_REGIONTYPE,
identityPoolId: Constants.COGNITO_IDENTITY_POOL_ID,
identityProviderManager: customProviderManager)


Could you convert these codes to Objective-C and tell me how to use converted codes in my above code?
Or please tell me the official recommended code?

Answer

Finally I figured out how to solve this issue a few days ago.

1.Add this class written in Swift to your Objc project.

// CognitoCustomProviderManager.swift

import Foundation
import AWSCognitoIdentityProvider

class MyProvider:NSObject, AWSIdentityProviderManager{
  var tokens : [NSString : NSString]?
  init(tokens: [NSString : NSString]) {
    self.tokens = tokens
    print("tokens : ", self.tokens);
  }
  @objc func logins() -> AWSTask {
    return AWSTask(result: tokens)
  }
}

2.In your view controller.

@property MyProvider *myProvider;

3.Initialze AWSCognitoCredentialsProvider with MyProvider that should be initialized with tokens.

MyProvider *Provider = [[MyProvider alloc] initWithTokens:@{AWSIdentityProviderFacebook : token }];
AWSCognitoCredentialsProvider *credentialsProvider = [[AWSCognitoCredentialsProvider alloc] initWithRegionType:COGNITO_REGION_TYPE identityPoolId:IDENTITY_POOL_ID identityProviderManager:Provider];

*If you want to write MyProvider in Objc.According to {yourProjectName}-Swift.h which is created once you add Swift file, Maybe this should work? I haven't inspect if this code works though.

@interface MyProvider : NSObject <AWSIdentityProviderManager>
@property (nonatomic, copy) NSDictionary<NSString *, NSString *> * _Nullable tokens;
- (nonnull instancetype)initWithTokens:(NSDictionary<NSString *, NSString *> * _Nonnull)tokens OBJC_DESIGNATED_INITIALIZER;
- (AWSTask * _Nonnull)logins;
@end
Comments