本文轉載至 http://blog.csdn.net/jinkaiouyang/article/details/35555123xcode
IOS8將指紋識別技術開放出來了。咱們可以利用用戶設置的touch ID來進行用戶鑑權。
TouchID的API主要集成在LocalAuthentication.framework中。將改framework加入到工程中,而且須要iPhone5S和IOS8系統的支持,就能使用TouchID的APIapp
了。oop

TouchID的API很是簡單:ui
一、使用以前,須要先判斷TouchID是否支持lua
- - (BOOL)canEvaluatePolicy:(LAPolicy)policy error:(NSError * __autoreleasing *)error;
二、調用TouchID識別接口
- - (void)evaluatePolicy:(LAPolicy)policy localizedReason:(NSString *)localizedReason reply:(void(^)(BOOL success, NSError *error))reply;
policy枚舉,表示須要使用到的權限,目前只有一個Biometrics,意思指的就是TouchID
- {
-
-
- LAPolicyDeviceOwnerAuthenticationWithBiometrics = kLAPolicyDeviceOwnerAuthenticationWithBiometrics
- } NS_ENUM_AVAILABLE(10_10, 8_0);
localizedReasonspa
使用TouchID的理由,會出如今彈框中,提示用戶。這個字段必須填寫且不爲nil或空值,不然會拋NSInvalidArgumentException異常,
(void(^)(BOOL success,NSError *error)).net
TouchID輸入的結果回調。使用block方式。
示例代碼:code
- LAContext *context = [[LAContext alloc] init];
- NSError *contextError = nil;
- NSString *localizedReasonString = @"Need Authorize";
-
- if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&contextError]) {
- [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics
- localizedReason:localizedReasonString
- reply:^(BOOL success, NSError *error) {
- NSString *title, *message;
-
- if (success) {
- title = @"Authorize Success";
- message = nil;
- } else {
- title = @"Authorize Faied";
- message = error.localizedFailureReason;
- }
-
- UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title
- message:message
- delegate:nil
- cancelButtonTitle:@"OK"
- otherButtonTitles:nil];
- [alertView show];
- }];
- } else {
- UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Context Error"
- message:contextError.localizedFailureReason
- delegate:nil
- cancelButtonTitle:@"OK"
- otherButtonTitles:nil];
- [alertView show];