Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- dismiss(animated: true, completion: {
- UserDefaults.standard.set(false, forKey: "hasLoginKey")
- })
- @IBAction func loginButtonPressed(_ sender: Any) {
- //Define Button variable from the button that has been tapped.
- let button = sender as! UIButton
- //If the button tag is Touch ID, authenticate the user
- if(button.tag == loginWithTouchID)
- {
- //Check if device is compatible with Touch ID
- if(touchMe.canEvaluatePolicy())
- {
- //Get Response from Touch ID popup
- touchMe.authenticateUser() { responsCode in
- if let responsCode = responsCode {
- if(responsCode == 0)
- {
- //If Touch ID is not available
- self.customAlert(title: "Error", message: "Touch ID not available")
- }
- else if(responsCode == 1)
- {
- //If Touch ID has not been setup
- self.customAlert(title: "Error", message: "Touch ID may not be configured")
- }
- else if(responsCode == 2)
- {
- //If Touch ID authentication failed
- self.customAlert(title: "Error", message: "There was a problem verifying your identity")
- }
- } else {
- //If there is no response code, that means Touch ID was successful in authenticating user and we can now call the login method
- Timer.scheduledTimer(timeInterval: 0.4, target: self, selector: #selector(Login.login), userInfo: nil, repeats: false)
- }
- }
- }
- }
- else
- {
- Timer.scheduledTimer(timeInterval: 0.4, target: self, selector: #selector(Login.login), userInfo: nil, repeats: false)
- }
- }
- class TouchIDAuth {
- let context = LAContext()
- func canEvaluatePolicy() -> Bool {
- return context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: nil)
- }
- func authenticateUser(completion: @escaping (NSNumber?) -> Void) {
- guard canEvaluatePolicy() else {
- completion(0)
- return
- }
- context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: "Logging in with Touch ID") { (success, evaluateError) in
- if success {
- DispatchQueue.main.async {
- completion(nil)
- }
- } else {
- let response: NSNumber
- switch evaluateError?._code {
- case Int(kLAErrorAuthenticationFailed):
- response = 2
- case Int(kLAErrorUserCancel):
- response = 3
- case Int(kLAErrorUserFallback):
- response = 4
- default:
- response = 1
- }
- completion(response)
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement