![](/img/trans.png)
[英]Swift: How to initialise a let constant in a UIViewController subclass
[英]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.