我正在尝试使用 swift 和 firebase 更改(编辑)iOS 应用程序中的用户电子邮件。 在更改电子邮件功能时,我添加了一些错误并针对每个错误发出警报。
当我模拟并测试错误时,例如,我尝试输入无效的电子邮件,错误正常工作! 但警报没有出现,它给了我这个错误,如下图所示。
代码如下:
if EmailAddressTF.text=="" || PasswordTF.text=="" || BusinessNameTF.text==""
{
let alert = UIAlertController(title: "Oops!", message:"Make sure to fill in all required textfields", preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "OK", style: .Default) { _ in })
self.presentViewController(alert, animated: true){}
}
else {
if RepasswordTF.text != PasswordTF.text
{
let alert = UIAlertController(title: "Oops!", message:"You Entered Different Passwords", preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "OK", style: .Default) { _ in })
self.presentViewController(alert, animated: true){}
}
else {
let ref = Firebase(url: "https://businesswallet.firebaseio.com/")
let mainpass = NSUserDefaults.standardUserDefaults().objectForKey("password") as? String
let mainEmail = NSUserDefaults.standardUserDefaults().objectForKey("Email") as? String
let isEqu = (mainEmail == self.EmailAddressTF.text)
if(isEqu == true)
{ }
else
{
ref.changeEmailForUser(mainEmail, password: mainpass, toNewEmail: self.EmailAddressTF.text, withCompletionBlock: { error in
if error != nil {
if let errorCode = FAuthenticationError(rawValue: error.code) {
switch (errorCode) {
case .EmailTaken:
print("Email taken")
let alert = UIAlertController(title: "Oops!", message:"Sorry,Email taken", preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "OK", style: .Default) { _ in })
self.presentViewController(alert, animated:true){}
case .InvalidEmail:
print("invalid email")
let alert = UIAlertController(title: "Oops!", message:"invalid email", preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "OK", style: .Default) { _ in })
self.presentViewController(alert, animated:true){}
case .NetworkError:
print("Network Error")
let alert = UIAlertController(title: "Oops!", message:"Network error, check your connection", preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "OK", style: .Default) { _ in })
self.presentViewController(alert, animated: true){}
default:
print("Unknown Error")
let alert = UIAlertController(title: "Oops!", message:"Unknown Error", preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "OK", style: .Default) { _ in })
self.presentViewController(alert, animated: true){}
}
}
} else {
NSUserDefaults.standardUserDefaults().setValue(self.EmailAddressTF.text, forKey: "Email")
print("Email changed successfully")
}
})
} //Big Else
我该如何解决这个问题?
最佳答案
尝试在主线程中呈现警报
dispatch_async(dispatch_get_main_queue()) {
let alertController = UIAlertController(title: head, message: message, preferredStyle: .Alert)
let OKAction = UIAlertAction(title: "OK", style: .Default) { (action:UIAlertAction!) in
print("you have pressed OK button");
}
alertController.addAction(OKAction)
self.presentViewController(alertController, animated: false, completion:nil)
}
关于ios - 警报 Controller 不出现错误! Swift 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36654056/