iT邦幫忙

2022 iThome 鐵人賽

DAY 27
0
Mobile Development

iOS菜逼八連續30天挑戰-2系列 第 27

iOS菜逼八連續30天挑戰-2 用Firebase當作登入系統-4

  • 分享至 

  • xImage
  •  

登入篇:
今天會講到如何用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)
                    }
                }
            }
    }


完成後:


上一篇
iOS菜逼八連續30天挑戰-2 用Firebase當作登入系統-3
下一篇
iOS菜逼八連續30天挑戰-2 用Firebase當作登入系統-5
系列文
iOS菜逼八連續30天挑戰-230
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言