我是新手,我一直在努力学习自己。我遇到了不确定如何解决的问题。代码如下:
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函数中的所有errorMsg都可以正常工作,但对于基于FIRAuth提供的error.code生成的少数errorMsgs,则不会出现。
我四处阅读,发现可能是因为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)
}
}
}
LoginViewController
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)
}
}
最佳答案
从我的评论中,我提到您将有两种可能的解决方案。最佳选择将取决于您要实现的目标。但我确信您可以使其与任何一个一起使用。
与Completion Handler
func userLogin(completion:(message:String)->()){
var errorMsg:String = ""
if (email == ""){
...
//check if found any errors yet
}else if (errorMsg != ""){
completion(errorMsg)
} else {
FIRAuth.auth()?.signInWithEmail(email!, password: password!){ (user, error) in
if (error != nil){
...
} else {
errorMsg = "You have successfully Logged In"
}
completion(errorMsg)
}
}
}
userLogin(){ (message:String) in
// this will only be called when userLogin trigger completion(errorMsg)...
print(message)
}
与自我
func userLogin() -> Void{
var errorMsg:String = ""
if (email == ""){
...
//check if found any error yet
}else if (errorMsg != ""){
self.errorMsg = errorMsg
} else {
FIRAuth.auth()?.signInWithEmail(email!, password: password!){ (user, error) in
if (error != nil){
...
} else {
errorMsg = "You have successfully Logged In"
}
self.errorMsg = errorMsg
}
}
}
关于ios - Swift:我无法将Firebase FIRAuth函数中的变量传递回父函数中的声明变量,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38293785/