登入篇:
今天會講到如何用email登入:
code如下:
if (accountTextField.text == "") {
CustomFunc.customAlert(title: "請輸入帳號!", message: "", vc: self, actionHandler: nil)
} else {
Auth.auth().createUser(withEmail:accountTextField.text!, password: passwordTextField.text!) { (user, error) in
if (error == nil) {
CustomFunc.customAlert(title: "帳號已成功建立!", message: "", vc: self, actionHandler: nil)
} else {
CustomFunc.customAlert(title: "", message: "\(String(describing: error!.localizedDescription))", vc: self, actionHandler: nil)
}
}
}
CustomFunc代碼如下:
class CustomFunc {
class func customAlert(title: String, message: String, vc: UIViewController, actionHandler: (() -> Void)?) {
let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
let closeAction = UIAlertAction(title: "關閉", style: .default) { action in
actionHandler?()
}
alertController.addAction(closeAction)
vc.present(alertController, animated: true)
}
}
整個畫面代碼如下:
@IBOutlet weak var signInBtn: UIButton!
@IBOutlet weak var signUpBtn: UIButton!
@IBOutlet weak var accountTextField: UITextField!
@IBOutlet weak var passwordTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
accountTextField.attributedPlaceholder = NSAttributedString(string:"Email or Phone Number.. ", attributes:[NSAttributedString.Key.foregroundColor: UIColor.lightGray])
passwordTextField.attributedPlaceholder = NSAttributedString(string:"Password..", attributes:[NSAttributedString.Key.foregroundColor: UIColor.lightGray])
accountTextField.backgroundColor = .black
passwordTextField.backgroundColor = .black
// Do any additional setup after loading the view.
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
view.endEditing(true)
}
@IBAction func signIn(_ sender: Any) {
}
@IBAction func signUp(_ sender: Any) {
if (accountTextField.text == "") {
CustomFunc.customAlert(title: "請輸入帳號!", message: "", vc: self, actionHandler: nil)
} else {
Auth.auth().createUser(withEmail:accountTextField.text!, password: passwordTextField.text!) { (user, error) in
if (error == nil) {
CustomFunc.customAlert(title: "帳號已成功建立!", message: "", vc: self, actionHandler: nil)
} else {
CustomFunc.customAlert(title: "", message: "\(String(describing: error!.localizedDescription))", vc: self, actionHandler: nil)
}
}
}
}
完成後: