Reputation: 15
Ok so when I run my app on my iPhone it allows me to enter information in the text fields but crashes when I tap on the 'Sign Up' button.
This is showAlert function in the view controller:
2016-12-12 20:24:37.943 Elixr[2013:473247] AWSiOSSDK v2.4.12 [Debug] AWSURLResponseSerialization.m line:63 | -[AWSJSONResponseSerializer responseObjectForResponse:originalRequest:currentRequest:data:error:] | Response body: {"__type":"NotAuthorizedException","message":"Unable to verify secret hash for client 5pmvgst5c9650q4v9dfii8g8h7"} Could not cast value of type 'NSError' (0x1a7abc0f0) to 'NSString' (0x1a7abc398).
2016-12-12 20:24:37.955718 Elixr[2013:472573] Could not cast value of type 'NSError' (0x1a7abc0f0) to 'NSString' (0x1a7abc398).
This is what the IBAction for the 'Sign Up' button:
@IBAction func SignUpButton(_ sender: Any) {
guard let usernameValue = self.Username.text, !usernameValue.isEmpty,
let passwordValue = self.Password.text, !passwordValue.isEmpty else {
showAlert(title: "Missing required fields", message: "Please enter a username or password.")
return
}
var attributes = [AWSCognitoIdentityUserAttributeType]()
if let emailValue = self.Email.text, !emailValue.isEmpty {
let email = AWSCognitoIdentityUserAttributeType()
email?.name = "email"
email?.value = emailValue
attributes.append(email!)
}
// Sign up the user
self.pool?.signUp(usernameValue, password: passwordValue, userAttributes: attributes, validationData: nil).continue ({[weak self] (task: AWSTask ) -> AnyObject? in
guard let strongSelf = self else { return nil }
DispatchQueue.main.async(execute: {
if let error = task.error {
self?.showAlert(title: "Error", message: error as! String) ---> ERROR OCCURS ON THIS LINE <---
} else if let result = task.result {
// handle the case where user has to confirm his identity via email / SMS
if (result.user.confirmedStatus != AWSCognitoIdentityUserStatus.confirmed) {
strongSelf.sentTo = result.codeDeliveryDetails?.destination
strongSelf.performSegue(withIdentifier: "SignUpConfirmSegue", sender:sender)
} else {
self?.showAlert(title: "Registration Complete", message: "Registration was successful.")
strongSelf.presentingViewController?.dismiss(animated: true, completion: nil)
}
}
})
return nil
})
This is the showAlert function:
func showAlert(title : String, message : String){
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: { _ in
}))
self.present(alert, animated: true, completion: nil)
}
Upvotes: 0
Views: 446
Reputation: 4849
A general good approach is to handle errors internally and show the user only "user friendly" messages based on what caused that error (eg. We are sorry but it appears.../ please try again.../ enter your password in order to login/ no internet/...)
Apple's documentation states the following
Use the forced form of the type cast operator (as!) only when you are sure that the downcast will always succeed. This form of the operator will trigger a runtime error if you try to downcast to an incorrect class type.
var localizedDescription: String A string containing the localized description of the error.
showAlert(title: "Error", message: error.localizedDescription)
Upvotes: 3
Reputation: 72420
Use localizedDescription
property of (NS)Error
to display error message.
self?.showAlert(title: "Error", message: error.localizedDescription)
Upvotes: 1