繁体   English   中英

如何在 Swift 中初始化 AWSCognitoIdentityUserAttributeType?

[英]How to initialise a AWSCognitoIdentityUserAttributeType in Swift?

我对 iOS/Swift 开发非常陌生。 我正在尝试使用AWS Cognito实现用户注册/登录。 使用默认配置初始化 SDK 后,我执行以下操作:

let pool = AWSCognitoIdentityUserPool(forKey: "UserPool")

[其次是]

pool.signUp(...) 

注册用户。

我需要将AWSCognitoIdentityUserAttributeType和数组传递给 signUp 方法,但我无法初始化这种类型的对象。

var first_name = AWSCognitoIdentityUserAttributeType(
                                 dictionary: ["given_name": "rob"])

导致以下错误:

2016-07-10 08:48:43.025 Demo1[4199:111322] -
         [AWSCognitoIdentityUserAttributeType initWithDictionary:]: 
         unrecognized selector sent to instance 0x7fee54a6ede0

我相信你应该这样做,创建一个 AWSCognitoIdentityUserAttributeType 数组,然后创建一堆该类型的对象,分配“名称”然后分配“值”,如下所示。 然后,您可以在 pool.signup 中使用该对象“属性”

    var attributes = [AWSCognitoIdentityUserAttributeType]()
    let name = AWSCognitoIdentityUserAttributeType()
    let email = AWSCognitoIdentityUserAttributeType()
    let gender = AWSCognitoIdentityUserAttributeType()
    let birthdate = AWSCognitoIdentityUserAttributeType()
    let username = AWSCognitoIdentityUserAttributeType()

    let myFormatter = DateFormatter()
    myFormatter.dateFormat = "dd/MM/yyyy"

    name?.name = "name"
    email?.name = "email"
    gender?.name = "gender" //implementar genero ainda
    birthdate?.name = "birthdate"
    sobrenome?.name = "family_name"
    username?.name = "preferred_username"


    let birthdateNsDate:Date = birthdateField.date as Date
    birthdate?.value = myFormatter.string(from: birthdateNsDate)
    name?.value = nameTextField.text!
    email?.value = emailTextField.text!


    attributes.append(name!)
    attributes.append(email!)
    attributes.append(gender!)
    attributes.append(birthdate!)
    attributes.append(sobrenome!)
    attributes.append(username!)

单个实例:

例如,

AWSCognitoIdentityUserAttributeType(name: "key", value: "string")

数组作为函数调用中的参数:

awsUserPool.currentUser()?.update([
    AWSCognitoIdentityUserAttributeType(name: "key0", value: string0),
    AWSCognitoIdentityUserAttributeType(name: "key1", value: string1)
])

值需要转换为字符串。 例如,

    AWSCognitoIdentityUserAttributeType(name: "key", value: "\(number)")

以下片段演示了如何使用

AWSCognitoIdentityUserAttributeType * phone = [AWSCognitoIdentityUserAttributeType new];
phone.name = @"phone_number";
//phone number must be prefixed by country code
phone.value = @"+15555555555";
AWSCognitoIdentityUserAttributeType * email = [AWSCognitoIdentityUserAttributeType new];
email.name = @"email";
email.value = @"email@mydomain.com";

//sign up the user
[[pool signUp:@"username" password:@"password" userAttributes:@[email,phone] validationData:nil] continueWithBlock:^id _Nullable(AWSTask<AWSCognitoIdentityUserPoolSignUpResponse *> * _Nonnull task) {
    dispatch_async(dispatch_get_main_queue(), ^{
        if(task.error){
            [[[UIAlertView alloc] initWithTitle:task.error.userInfo[@"__type"]
                                        message:task.error.userInfo[@"message"]
                                       delegate:self
                              cancelButtonTitle:@"Ok"
                              otherButtonTitles:nil] show];
        }else {
            AWSCognitoIdentityUserPoolSignUpResponse * response = task.result;
            if(!response.userConfirmed){
                //need to confirm user using user.confirmUser:
            }
        }});
    return nil;
}];

以下链接描述了用户池与 iOS SDK 的使用: https ://docs.aws.amazon.com/cognito/latest/developerguide/using-amazon-cognito-user-identity-pools-ios-sdk.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM