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

蓝咒 提交于 2019-12-12 19:49:08

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/38311479/iosaws-cognito-logins-is-deprecated-use-awsidentityprovidermanager

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!