iOS touchID的使用(指紋識(shí)別代替密碼)

#import <LocalAuthentication/LocalAuthentication.h>

//指紋識(shí)別的操作
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    //1. 判斷系統(tǒng)版本
    if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0) {
        
        //2. LAContext : 本地驗(yàn)證對(duì)象上下文
        LAContext *context = [LAContext new];
        
        //3. 判斷是否可用
        //Evaluate: 評(píng)估  Policy: 策略,方針
        //LAPolicyDeviceOwnerAuthenticationWithBiometrics: 允許設(shè)備擁有者使用生物識(shí)別技術(shù)
        if (![context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:nil]) {
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"對(duì)不起, 指紋識(shí)別技術(shù)暫時(shí)不可用" message:nil delegate:nil cancelButtonTitle:@"確定" otherButtonTitles: nil];
                [alertView show];
            });
            
        }
        
        //4. 開始使用指紋識(shí)別
        //localizedReason: 指紋識(shí)別出現(xiàn)時(shí)的提示文字, 一般填寫為什么使用指紋識(shí)別
        [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:@"開啟了指紋識(shí)別, 將打開隱藏功能" reply:^(BOOL success, NSError * _Nullable error) {
            
            if (success) {
                NSLog(@"指紋識(shí)別成功");
                // 指紋識(shí)別成功,回主線程更新UI,彈出提示框
                dispatch_async(dispatch_get_main_queue(), ^{
                    
                    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"指紋識(shí)別成功" message:nil delegate:nil cancelButtonTitle:@"確定" otherButtonTitles: nil];
                    [alertView show];
                });
                
            }
            
            if (error) {
                
                // 錯(cuò)誤的判斷chuli
                
                if (error.code == -2) {
                    
                    // 取消操作,回主線程更新UI,彈出提示框
                    dispatch_async(dispatch_get_main_queue(), ^{
                        
                        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"用戶取消了操作" message:nil delegate:nil cancelButtonTitle:@"確定" otherButtonTitles: nil];
                        [alertView show];
                        
                    });
                    
                } else {
                    NSLog(@"錯(cuò)誤: %@",error);
                    // 指紋識(shí)別出現(xiàn)錯(cuò)誤,回主線程更新UI,彈出提示框
                    dispatch_async(dispatch_get_main_queue(), ^{
                        
                        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"指紋驗(yàn)證失敗" delegate:nil cancelButtonTitle:@"確定" otherButtonTitles: nil];
                        [alertView show];
                        
                    });
                    
                }
                
            }
            
        }];
    } else {
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"對(duì)不起, 該手機(jī)不支持指紋識(shí)別" message:nil delegate:nil cancelButtonTitle:@"確定" otherButtonTitles: nil];
            [alertView show];
        });
        
    }
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容