[iOS][AWS Cognito] 'logins' is deprecated:

2019-07-19 08:44发布

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?

2条回答
迷人小祖宗
2楼-- · 2019-07-19 09:31

same issue here:||

I read the the solution example you posted, and I found that there is an updated example

Updated Example Link

The updated example is implement in Swift but this issue they solved and implemented by Object-C. Check the folder "DeveloperAuthenticated" in the updated example.

查看更多
beautiful°
3楼-- · 2019-07-19 09:36

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

I spent plenty of time to make it work. so I hope this post gonna be helpful for someone who have the same issue! thanks.

查看更多
登录 后发表回答