我們在日常生活中經(jīng)常會遇到使用軟件彈出Touch-ID驗證權限的問題续扔。
例如:
Touch-ID驗證.png
- 其實很簡單的只要我們引入
LocalAuthentication.framework
框架即可年柠。 - 導入框架后我們需要在項目中引入頭文件
#import <LocalAuthentication/LocalAuthentication.h>
- 我們會發(fā)現(xiàn)
LAContext.h
中有兩個方法
- (BOOL)canEvaluatePolicy:(LAPolicy)policy error:(NSError * __autoreleasing *)error __attribute__((swift_error(none)));
- (void)evaluatePolicy:(LAPolicy)policy localizedReason:(NSString *)localizedReason reply:(void(^)(BOOL success, NSError * __nullable error))reply;
第一個方法官方解釋东跪,是判斷設備是否支持身份驗證扳炬,
policy
這個參數(shù)有兩個值:LAPolicyDeviceOwnerAuthentication
和LAPolicyDeviceOwnerAuthenticationWithBiometrics
吓懈。根據(jù)翻譯大概能猜出第一個是純粹的的身份驗證锄奢,第二個參數(shù)是生物身份驗證(這個要比身份驗證更安全更高級*)失晴,代碼比較簡單剧腻,所以就不上代碼了,末尾會有GitHub 地址涂屁。
- 這里有一個強調的點就是有人獲取權限驗證的時候會手機會卡屏书在,其實是因為驗證方法不是放在主線程的,所有我們獲取返回結果的時候拆又,我們需要放到主線程進行處理儒旬,相關官方解釋:
*The method does not block. Instead, the caller must provide a reply block to be
called asynchronously when evaluation finishes. The block is executed on a private
queue internal to the framework in an unspecified threading context. Other than that, no guarantee is made about which queue, thread, or run-loop the block is executed on.