Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.12 KB | None | 0 0
  1. dismiss(animated: true, completion: {
  2.  
  3. UserDefaults.standard.set(false, forKey: "hasLoginKey")
  4.  
  5. })
  6.  
  7. @IBAction func loginButtonPressed(_ sender: Any) {
  8.  
  9. //Define Button variable from the button that has been tapped.
  10. let button = sender as! UIButton
  11.  
  12. //If the button tag is Touch ID, authenticate the user
  13.  
  14. if(button.tag == loginWithTouchID)
  15. {
  16. //Check if device is compatible with Touch ID
  17. if(touchMe.canEvaluatePolicy())
  18. {
  19. //Get Response from Touch ID popup
  20. touchMe.authenticateUser() { responsCode in
  21.  
  22. if let responsCode = responsCode {
  23.  
  24. if(responsCode == 0)
  25. {
  26. //If Touch ID is not available
  27. self.customAlert(title: "Error", message: "Touch ID not available")
  28. }
  29. else if(responsCode == 1)
  30. {
  31. //If Touch ID has not been setup
  32. self.customAlert(title: "Error", message: "Touch ID may not be configured")
  33. }
  34. else if(responsCode == 2)
  35. {
  36. //If Touch ID authentication failed
  37. self.customAlert(title: "Error", message: "There was a problem verifying your identity")
  38. }
  39.  
  40. } else {
  41.  
  42. //If there is no response code, that means Touch ID was successful in authenticating user and we can now call the login method
  43. Timer.scheduledTimer(timeInterval: 0.4, target: self, selector: #selector(Login.login), userInfo: nil, repeats: false)
  44. }
  45. }
  46. }
  47. }
  48. else
  49. {
  50. Timer.scheduledTimer(timeInterval: 0.4, target: self, selector: #selector(Login.login), userInfo: nil, repeats: false)
  51. }
  52.  
  53.  
  54. }
  55.  
  56. class TouchIDAuth {
  57.  
  58. let context = LAContext()
  59.  
  60. func canEvaluatePolicy() -> Bool {
  61. return context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: nil)
  62. }
  63.  
  64. func authenticateUser(completion: @escaping (NSNumber?) -> Void) {
  65.  
  66. guard canEvaluatePolicy() else {
  67. completion(0)
  68. return
  69. }
  70.  
  71. context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: "Logging in with Touch ID") { (success, evaluateError) in
  72. if success {
  73. DispatchQueue.main.async {
  74. completion(nil)
  75. }
  76. } else {
  77.  
  78. let response: NSNumber
  79.  
  80. switch evaluateError?._code {
  81. case Int(kLAErrorAuthenticationFailed):
  82. response = 2
  83. case Int(kLAErrorUserCancel):
  84. response = 3
  85. case Int(kLAErrorUserFallback):
  86. response = 4
  87. default:
  88. response = 1
  89. }
  90.  
  91. completion(response)
  92.  
  93. }
  94. }
  95. }
  96.  
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement