[iOS] [AWS Cognito]'logins'已过时:使用“ AWSIdentityProviderManager” [英] [iOS][AWS Cognito] 'logins' is deprecated: Use "AWSIdentityProviderManager"

查看:329
本文介绍了[iOS] [AWS Cognito]'logins'已过时:使用“ AWSIdentityProviderManager”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试通过Amazon Cognito在iOS上使用Facebook和Twitter对用户进行身份验证。
由于官方文档较旧,我无法执行。

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.

这是我的代码:

   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;

但是Xcode说'logins'已过时:使用 AWSIdentityProviderManager为凭证提供者提供有效的登录字典

我发现,不赞成使用登录名时,proxyProvider.logins返回[null]。

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

Amazon官方文档(英语,日语)和样本不是最新的,所以我不知道如何正确实施以对用户进行身份验证。

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

最后,我在Swift中找到了解决方案,但我不知道

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)

能否将这些代码转换为Objective-C,并告诉我如何在上面的代码中使用转换后的代码?
还是请告诉我官方推荐的代码?

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?

推荐答案

最后我终于想通了几天前。

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

1。将用Swift编写的此类添加到Objc项目中。

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。在您的视图控制器中。

2.In your view controller.

@property MyProvider *myProvider;

3。使用MyProvider初始化AWSCognitoCredentialsProvider,并使用令牌对其进行初始化。

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];

*如果要在Objc中编写MyProvider。根据添加您的Swift文件后创建的 {yourProjectName} -Swift.h ,也许这应该有用吗?我尚未检查此代码是否有效。

*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.

这篇关于[iOS] [AWS Cognito]'logins'已过时:使用“ AWSIdentityProviderManager”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆