2017-07-17 63 views
0

我目前的項目有一個登錄頁面,但我發現用戶必須在登錄時再次輸入他們的用戶名和密碼。通過實現「記住用戶名和密碼」功能並添加Touch ID,對他們更容易些嗎?Xcode 8.3.3:如何記住用戶名和密碼並添加Touch-ID身份驗證

我的代碼如下:

進口的UIKit

進口火力地堡

類LoginViewController:UIViewController的{

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) 
      } 
     } 
    } 
} 

回答

0

你有一對夫婦的方式來保存您的用戶憑據。最常見的方法是將它們保存在鑰匙串中。

KeyChain Swift

之後,你可以用戶的touchID認證,通過調用默認的方法來實現這一點。這裏是一個很好的教程你應該怎麼做:

Touch ID for Local Authentication

相關問題