O2BioLocalAuth.swift 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. //
  2. // O2BioLocalAuth.swift
  3. // O2BioLocalAuth
  4. //
  5. // Created by FancyLou on 2019/3/8.
  6. // Copyright © 2019 O2OA. All rights reserved.
  7. //
  8. import Foundation
  9. import LocalAuthentication
  10. import CocoaLumberjack
  11. class O2BioLocalAuth: NSObject {
  12. static let shared: O2BioLocalAuth = O2BioLocalAuth()
  13. private override init() {}
  14. // 检查当前设备支持的生物识别类型
  15. func checkBiometryType() -> O2BiometryType {
  16. if #available(iOS 8.0, *) {
  17. let context = LAContext.init()
  18. if(context.canEvaluatePolicy(LAPolicy.deviceOwnerAuthenticationWithBiometrics, error: nil)){
  19. if #available(iOS 11.0, *) {
  20. switch(context.biometryType) {
  21. case LABiometryType.faceID:
  22. return (O2BiometryType.FaceID)
  23. case LABiometryType.touchID:
  24. return (O2BiometryType.TouchID)
  25. case LABiometryType.none:
  26. return (O2BiometryType.None)
  27. }
  28. }else {
  29. return (O2BiometryType.TouchID)
  30. }
  31. }else{
  32. return (O2BiometryType.None)
  33. }
  34. }else {
  35. return (O2BiometryType.None)
  36. }
  37. }
  38. func auth(reason: String, selfAuthTitle: String, block:@escaping (O2BioEvaluateResult, String)->Void) {
  39. if #available(iOS 8.0, *) {
  40. let context = LAContext.init()
  41. context.localizedFallbackTitle = selfAuthTitle
  42. if(context.canEvaluatePolicy(LAPolicy.deviceOwnerAuthenticationWithBiometrics, error: nil)){
  43. context.evaluatePolicy(LAPolicy.deviceOwnerAuthenticationWithBiometrics, localizedReason: reason, reply: { (success, error) in
  44. if success {
  45. DispatchQueue.main.async {
  46. block(O2BioEvaluateResult.SUCCESS, "认证成功")
  47. }
  48. }else {
  49. if error != nil && error is LAError {
  50. let code = (error as! LAError).code
  51. // LAError.appCancel //应用取消
  52. // LAError.authenticationFailed //认证失败
  53. // LAError.biometryLockout //生物识别功能锁定,一般是验证错误次数超过5次
  54. // LAError.biometryNotAvailable //设备不支持
  55. // LAError.biometryNotEnrolled // 未启用生物识别功能
  56. // LAError.invalidContext // context失效
  57. // LAError.passcodeNotSet //系统没有设置密码,所以无法启用生物识别验证
  58. // LAError.systemCancel //系统取消了验证,有可能是其他应用到前台了
  59. // LAError.userCancel // 用户点击了取消按钮
  60. // LAError.userFallback // 用户点击了自定义处理的按钮
  61. DDLogDebug("error:\(error?.localizedDescription ?? "")")
  62. if #available(iOS 11.0, *) {
  63. switch code {
  64. case LAError.userFallback:
  65. DDLogDebug("用户需要自定义处理")
  66. DispatchQueue.main.async {
  67. block(O2BioEvaluateResult.FALLBACK, "用户自定义处理")
  68. }
  69. break
  70. case LAError.biometryLockout:
  71. DDLogDebug("锁定了")
  72. DispatchQueue.main.async {
  73. block(O2BioEvaluateResult.LOCKED, "无法使用,识别功能已经锁定了")
  74. }
  75. break
  76. default:
  77. DispatchQueue.main.async {
  78. block(O2BioEvaluateResult.FAILURE, error?.localizedDescription ?? "")
  79. }
  80. break;
  81. }
  82. }else {
  83. switch code {
  84. case LAError.userFallback:
  85. DDLogDebug("用户需要自定义处理")
  86. DispatchQueue.main.async {
  87. block(O2BioEvaluateResult.FALLBACK, "用户自定义处理")
  88. }
  89. break
  90. case LAError.touchIDLockout:
  91. DDLogDebug("锁定了")
  92. DispatchQueue.main.async {
  93. block(O2BioEvaluateResult.LOCKED, "无法使用,识别功能已经锁定了")
  94. }
  95. break
  96. default:
  97. DispatchQueue.main.async {
  98. block(O2BioEvaluateResult.FAILURE, error?.localizedDescription ?? "")
  99. }
  100. break;
  101. }
  102. }
  103. }else {
  104. DDLogError("error: \(error?.localizedDescription ?? "")")
  105. DispatchQueue.main.async {
  106. block(O2BioEvaluateResult.FAILURE, "其它异常,\(error?.localizedDescription ?? "")")
  107. }
  108. }
  109. }
  110. })
  111. }else {
  112. DispatchQueue.main.async {
  113. DDLogDebug("error 设备不支持!")
  114. block(O2BioEvaluateResult.FAILURE, "设备不支持")
  115. }
  116. }
  117. }else {
  118. DispatchQueue.main.async {
  119. DDLogDebug("error 设备不支持!")
  120. block(O2BioEvaluateResult.FAILURE, "设备不支持")
  121. }
  122. }
  123. }
  124. }
  125. enum O2BiometryType {
  126. case None // 不支持
  127. case FaceID //面容识别
  128. case TouchID //指纹识别
  129. }
  130. // 认证结果
  131. enum O2BioEvaluateResult {
  132. case SUCCESS
  133. case FALLBACK
  134. case LOCKED
  135. case FAILURE
  136. }