【发布时间】:2016-11-12 14:58:31
【问题描述】:
我是 swift 的新手,我一直在努力学习自己。我遇到了一个问题,我不确定如何解决它。代码如下:
import Foundation
import Firebase
class UserLogin {
var email:String?
var password:String?
init(email:String, password:String){
self.email = email
self.password = password
}
func userLogin() -> String{
var errorMsg:String = ""
//Email & Password Integrity check
if (email == ""){
errorMsg = "Please enter your email"
} else if (email?.rangeOfString("@") == nil || email?.rangeOfString(".") == nil){
errorMsg = "Email is invalid"
}else if (password == ""){
errorMsg = "Please enter your password"
} else if (password?.characters.count < 8){
errorMsg = "Password is invalid"
}else{
print("Logging In... with Email:\(email!) and Password:\(password!)")
//Firebase Authentication Process"
FIRAuth.auth()?.signInWithEmail(email!, password: password!){ (user, error) in
// ...
if (error != nil){
let errorCode = error!.code
if (errorCode == 17009){
errorMsg = "You have entered the wrong password"
} else if (errorCode == 17011){
errorMsg = "Your email does not exist"
} else if (errorCode == 17010) {
errorMsg = "You have tried to login too many times with the wrong credentials. Please try again later."
} else {
print(error)
}
} else {
print("User is Logged In")
errorMsg = "You have successfully Logged In"
}
}
}
return errorMsg
}
}
基本上在我的 ViewController 中,我有一个像这样工作的单独代码
let alert = UIAlertController(title: "Error", message: errorMsg, preferredStyle: .Alert)
let action = UIAlertAction(title: "OK", style: .Default, handler: nil)
alert.addAction(action)
self.presentViewController(alert, animated: true, completion: nil)
这适用于我的 userLogin func 中的所有 errorMsg,但对于基于 FIRAuth 提供的 error.code 生成的少数 errorMsg,它不会出现。
我阅读并认为这可能是因为 FIRAuth 是一个异步调用,但我不知道如何解决它。
很抱歉,如果这听起来很愚蠢,但我整天都在想办法但无济于事,如果能得到你们的帮助真是太棒了。
补充: 我按照建议实现了 CompletionHandler,但我不明白为什么它不起作用,尽管它应该......以下是我的代码。
UserLogin1.swift
import Foundation
import Firebase
class UserLogin1 {
var email:String?
var password:String?
init(email:String, password:String){
self.email = email
self.password = password
}
func userLogin(completion:(message:String)->()) {
var errorMsg:String = ""
//Email & Password Integrity check
if (email == ""){
errorMsg = "Please enter your email"
} else if (email?.rangeOfString("@") == nil || email?.rangeOfString(".") == nil){
errorMsg = "Email is invalid"
}else if (password == ""){
errorMsg = "Please enter your password"
} else if (password?.characters.count < 8){
errorMsg = "Password is invalid"
}else if (errorMsg != ""){
completion(message: errorMsg)
}else{
print("Logging In... with Email:\(email!) and Password:\(password!)")
//Firebase Authentication Process"
FIRAuth.auth()?.signInWithEmail(email!, password: password!){ (user, error) in
// ...
if (error != nil){
let errorCode = error!.code
if (errorCode == 17009){
errorMsg = "You have entered the wrong password"
} else if (errorCode == 17011){
errorMsg = "Your email does not exist"
} else if (errorCode == 17010) {
errorMsg = "You have tried to login too many times with the wrong credentials. Please try again later."
} else {
print(error)
}
} else {
print("User is Logged In")
errorMsg = "You have successfully Logged In"
}
}
completion(message: errorMsg)
}
}
}
登录视图控制器
import UIKit
class LoginViewController: UIViewController {
//Properties
@IBOutlet weak var passwordTextField: UITextField!
@IBOutlet weak var emailTextField: UITextField!
//Actions
@IBAction func loginButton(sender: AnyObject) {
let email = self.emailTextField.text!
let password = self.passwordTextField.text!
let user = UserLogin1(email: email, password: password)
user.userLogin(){ (message:String) in
print(message)
}
}
【问题讨论】:
-
当您推测问题在于
signInWithEmail是异步调用时,您是对的。这意味着当您返回消息时,登录功能甚至没有完成处理。您将能够使用completion handler 或仅在self. errorMsg中设置您的消息来完成此操作。
标签: ios iphone swift firebase firebase-authentication