首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Xcode 8.3.3:如何记住用户名和密码并添加Touch-ID身份验证

Xcode 8.3.3:如何记住用户名和密码并添加Touch-ID身份验证
EN

Stack Overflow用户
提问于 2017-07-17 11:12:56
回答 1查看 580关注 0票数 0

我当前的项目有一个登录页面,但我发现用户在登录时必须再次输入用户名和密码。我如何通过实现“记住用户名和密码”功能和添加Touch ID来让他们更容易?

我的代码如下:

导入UIKit

导入Firebase

类LoginViewController: UIViewController {

代码语言:javascript
复制
var imageView: UIImageView!

override func viewDidLoad() {
    super.viewDidLoad()
    imageView = UIImageView(frame: view.bounds)
    imageView.contentMode = .scaleAspectFill
    imageView.clipsToBounds = true
    imageView.image = #imageLiteral(resourceName: "background")
    imageView.center = view.center
    view.addSubview(imageView)
    self.view.sendSubview(toBack: imageView)

}

//Outlets
@IBOutlet weak var emailTextField: UITextField!
@IBOutlet weak var passwordTextField: UITextField!


//Login Action
@IBAction func loginAction(_ sender: AnyObject) {
    if self.emailTextField.text == "" || self.passwordTextField.text == "" {

        //Alert to tell the user that there was an error because they didn't fill anything in the textfields because they didn't fill anything in

        let alertController = UIAlertController(title: "Error", message: "Please enter an email and password.", preferredStyle: .alert)

        let defaultAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
        alertController.addAction(defaultAction)

        self.present(alertController, animated: true, completion: nil)

    } else {

        Auth.auth().signIn(withEmail: self.emailTextField.text!, password: self.passwordTextField.text!) { (user, error) in

            if error == nil {

                //Print into the console if successfully logged in
                print("You have successfully logged in")

                //Go to the HomeViewController if the login is sucessful
                let vc = self.storyboard?.instantiateViewController(withIdentifier: "Home")
                self.present(vc!, animated: true, completion: nil)

            } else {

                //Tells the user that there is an error and then gets firebase to tell them the error
                let alertController = UIAlertController(title: "Error", message: error?.localizedDescription, preferredStyle: .alert)

                let defaultAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
                alertController.addAction(defaultAction)

                self.present(alertController, animated: true, completion: nil)
            }
        }
    }
}
EN

回答 1

Stack Overflow用户

发布于 2017-07-17 16:12:15

有几种方法可以保存用户凭据。最常见的方法是将它们保存在Keychain中。

KeyChain Swift

之后,您可以通过调用默认方法来实现touchID身份验证。这是一个很好的教程,你应该怎么做:

Touch ID for Local Authentication

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45135444

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档