[英]How to initialise a AWSCognitoIdentityUserAttributeType in Swift?
I'm very new to iOS/Swift development.我对 iOS/Swift 开发非常陌生。 I'm trying to implement user signup/signin using AWS Cognito .我正在尝试使用AWS Cognito实现用户注册/登录。 After initialising the SDK with a default configuration I do:使用默认配置初始化 SDK 后,我执行以下操作:
let pool = AWSCognitoIdentityUserPool(forKey: "UserPool")
[followed by] [其次是]
pool.signUp(...)
to register a user.注册用户。
I need to pass and array of AWSCognitoIdentityUserAttributeType
to the signUp method, but I'm unable to initialise objects of this type.我需要将AWSCognitoIdentityUserAttributeType
和数组传递给 signUp 方法,但我无法初始化这种类型的对象。
var first_name = AWSCognitoIdentityUserAttributeType(
dictionary: ["given_name": "rob"])
results in the following Error:导致以下错误:
2016-07-10 08:48:43.025 Demo1[4199:111322] -
[AWSCognitoIdentityUserAttributeType initWithDictionary:]:
unrecognized selector sent to instance 0x7fee54a6ede0
I Belive you should do like this, create an Array of AWSCognitoIdentityUserAttributeType, then create a bunch of objects of that type, assign the 'name' then the 'value' as I did below.我相信你应该这样做,创建一个 AWSCognitoIdentityUserAttributeType 数组,然后创建一堆该类型的对象,分配“名称”然后分配“值”,如下所示。 Then you can use that object 'attributes' in your pool.signup然后,您可以在 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!)
A single instance:单个实例:
eg,例如,
AWSCognitoIdentityUserAttributeType(name: "key", value: "string")
An array as a parameter in a function call:数组作为函数调用中的参数:
awsUserPool.currentUser()?.update([
AWSCognitoIdentityUserAttributeType(name: "key0", value: string0),
AWSCognitoIdentityUserAttributeType(name: "key1", value: string1)
])
Values need to be converted to a String.值需要转换为字符串。 Eg,例如,
AWSCognitoIdentityUserAttributeType(name: "key", value: "\(number)")
Following snippet demonstrates how to use以下片段演示了如何使用
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;
}];
Following link describes usage of User Pools with the iOS SDK: https://docs.aws.amazon.com/cognito/latest/developerguide/using-amazon-cognito-user-identity-pools-ios-sdk.html以下链接描述了用户池与 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.