悟空记账

JZTouchIDTool.m 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. //
  2. // JZTouchIDTool.m
  3. // JIZHANG
  4. //
  5. // Created by xiaoxi on 2017/12/20.
  6. // Copyright © 2017年 kuxuan. All rights reserved.
  7. //
  8. #import "JZTouchIDTool.h"
  9. #import <UIKit/UIKit.h>
  10. #import <LocalAuthentication/LocalAuthentication.h>
  11. /*
  12. * 验证成功
  13. */
  14. NSString *const JZValidateTouchIDSuccess = @"JZValidateTouchIDSuccess";
  15. /*
  16. * 验证失败
  17. */
  18. NSString *const JZValidateTouchIDFailure = @"JZValidateTouchIDFailure";
  19. /*
  20. * 取消验证
  21. */
  22. NSString *const JZValidateTouchIDCancel = @"JZValidateTouchIDCancel";
  23. /*
  24. * 输入密码
  25. */
  26. NSString *const JZValidateTouchIDUserFallback = @"JZValidateTouchIDUserFallback";
  27. /*
  28. * 设备未设置密码
  29. */
  30. NSString *const JZValidateTouchIDPasscodeNotSet = @"JZValidateTouchIDPasscodeNotSet";
  31. /*
  32. * 设备指纹不可用
  33. */
  34. NSString *const JZValidateTouchIDNotAvailable = @"JZValidateTouchIDNotAvailable";
  35. /*
  36. * 设备未设置指纹
  37. */
  38. NSString *const JZValidateTouchIDNotEnrolled = @"JZValidateTouchIDNotEnrolled";
  39. /*
  40. * 设备指纹被锁定
  41. */
  42. NSString *const JZValidateTouchIDLockout = @"JZValidateTouchIDLockout";
  43. /*
  44. * 验证指纹被禁止
  45. */
  46. NSString *const JZValidateTouchIDInvalidContext = @"JZValidateTouchIDInvalidContext";
  47. @implementation JZTouchIDTool
  48. + (void)setNeedTouchIDYes {
  49. [[NSUserDefaults standardUserDefaults] setBool:YES forKey:JZNEEDTOUCHID];
  50. }
  51. + (void)setNeedTouchIDNo {
  52. [[NSUserDefaults standardUserDefaults] setBool:NO forKey:JZNEEDTOUCHID];
  53. }
  54. + (BOOL)needTouchID {
  55. return [[NSUserDefaults standardUserDefaults] boolForKey:JZNEEDTOUCHID];
  56. }
  57. + (void)openTouchID {
  58. [[NSUserDefaults standardUserDefaults] setBool:YES forKey:JZOPENTOUCHID];
  59. }
  60. + (void)closeTouchID {
  61. [[NSUserDefaults standardUserDefaults] setBool:NO forKey:JZOPENTOUCHID];
  62. }
  63. + (BOOL)isOpenTouchID {
  64. return [[NSUserDefaults standardUserDefaults] boolForKey:JZOPENTOUCHID];
  65. }
  66. + (BOOL)supportTouchID {
  67. if (@available(iOS 8.0, *)) {
  68. return YES;
  69. }
  70. else {
  71. return NO;
  72. }
  73. }
  74. + (BOOL)canTouchID {
  75. if (@available(iOS 8.0, *)) {
  76. // LAContext *context = [[LAContext alloc] init];
  77. // NSError *error = nil;
  78. // [context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error];
  79. // if (error.code == LAErrorPasscodeNotSet || error.code == LAErrorTouchIDNotEnrolled || error.code == LAErrorTouchIDNotAvailable) {
  80. // return NO;
  81. // }
  82. return YES;
  83. }
  84. else {
  85. return NO;
  86. }
  87. }
  88. + (void)validateTouchID {
  89. if (@available(iOS 8.0, *)) {
  90. LAContext *context = [[LAContext alloc] init];
  91. context.localizedFallbackTitle = @"";
  92. NSError *error = nil;
  93. if (@available(iOS 9.0, *)) {
  94. [context evaluatePolicy:LAPolicyDeviceOwnerAuthentication localizedReason:@"指纹解锁" reply:^(BOOL success, NSError * _Nullable error) {
  95. dispatch_async(dispatch_get_main_queue(), ^{
  96. if (error) {
  97. switch (error.code) {
  98. case LAErrorAuthenticationFailed:
  99. [[NSNotificationCenter defaultCenter] postNotificationName:JZValidateTouchIDFailure object:nil];
  100. break;
  101. case LAErrorUserCancel:
  102. [[NSNotificationCenter defaultCenter] postNotificationName:JZValidateTouchIDCancel object:nil];
  103. break;
  104. case LAErrorUserFallback:
  105. [[NSNotificationCenter defaultCenter] postNotificationName:JZValidateTouchIDUserFallback object:nil];
  106. break;
  107. case LAErrorSystemCancel:
  108. [[NSNotificationCenter defaultCenter] postNotificationName:JZValidateTouchIDCancel object:nil];
  109. break;
  110. case LAErrorPasscodeNotSet:
  111. [[NSNotificationCenter defaultCenter] postNotificationName:JZValidateTouchIDPasscodeNotSet object:nil];
  112. break;
  113. case LAErrorTouchIDNotAvailable:
  114. [[NSNotificationCenter defaultCenter] postNotificationName:JZValidateTouchIDNotAvailable object:nil];
  115. break;
  116. case LAErrorTouchIDNotEnrolled:
  117. [[NSNotificationCenter defaultCenter] postNotificationName:JZValidateTouchIDNotEnrolled object:nil];
  118. break;
  119. case LAErrorTouchIDLockout:
  120. {
  121. [[NSNotificationCenter defaultCenter] postNotificationName:JZValidateTouchIDLockout object:nil];
  122. // [self validateTouchID];
  123. }
  124. break;
  125. case LAErrorAppCancel:
  126. [[NSNotificationCenter defaultCenter] postNotificationName:JZValidateTouchIDCancel object:nil];
  127. break;
  128. case LAErrorInvalidContext:
  129. {
  130. [[NSNotificationCenter defaultCenter] postNotificationName:JZValidateTouchIDInvalidContext object:nil];
  131. // [self validateTouchID];
  132. }
  133. break;
  134. default:
  135. break;
  136. }
  137. }
  138. else {
  139. [[NSNotificationCenter defaultCenter] postNotificationName:JZValidateTouchIDSuccess object:nil];
  140. }
  141. });
  142. }];
  143. }
  144. else {
  145. [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:@"指纹解锁" reply:^(BOOL success, NSError * _Nullable error) {
  146. dispatch_async(dispatch_get_main_queue(), ^{
  147. if (error) {
  148. switch (error.code) {
  149. case LAErrorAuthenticationFailed:
  150. [[NSNotificationCenter defaultCenter] postNotificationName:JZValidateTouchIDFailure object:nil];
  151. break;
  152. case LAErrorUserCancel:
  153. [[NSNotificationCenter defaultCenter] postNotificationName:JZValidateTouchIDCancel object:nil];
  154. break;
  155. case LAErrorUserFallback:
  156. [[NSNotificationCenter defaultCenter] postNotificationName:JZValidateTouchIDUserFallback object:nil];
  157. break;
  158. case LAErrorSystemCancel:
  159. [[NSNotificationCenter defaultCenter] postNotificationName:JZValidateTouchIDCancel object:nil];
  160. break;
  161. case LAErrorPasscodeNotSet:
  162. [[NSNotificationCenter defaultCenter] postNotificationName:JZValidateTouchIDPasscodeNotSet object:nil];
  163. break;
  164. case LAErrorTouchIDNotAvailable:
  165. [[NSNotificationCenter defaultCenter] postNotificationName:JZValidateTouchIDNotAvailable object:nil];
  166. break;
  167. case LAErrorTouchIDNotEnrolled:
  168. [[NSNotificationCenter defaultCenter] postNotificationName:JZValidateTouchIDNotEnrolled object:nil];
  169. break;
  170. default:
  171. break;
  172. }
  173. }
  174. else {
  175. [[NSNotificationCenter defaultCenter] postNotificationName:JZValidateTouchIDSuccess object:nil];
  176. }
  177. });
  178. }];
  179. }
  180. }
  181. }
  182. @end