指紋解鎖主要代碼

 LAContext *myContext = [[LAContext alloc] init];
    // 這個屬性是設(shè)置指紋輸入失敗之后的彈出框的選項
    myContext.localizedFallbackTitle = @"忘記密碼";
    NSError *authError = nil;
    NSString *myLocalizedReasonString = @"請按住Home鍵完成驗證";
    // MARK: 判斷設(shè)備是否支持指紋識別
    if (![myContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&authError])
    {
        [myContext evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:myLocalizedReasonString reply:^(BOOL success, NSError * _Nullable error) {
            if(success)
            {
                NSLog(@"指紋認(rèn)證成功");
                UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"驗證結(jié)果" message:@"指紋認(rèn)證成功" preferredStyle:UIAlertControllerStyleAlert];
                
                UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                    
                }];
                [alertController addAction:cancelAction];
                [self presentViewController:alertController animated:YES completion:nil];
                
                
            }
            else
            {
                NSLog(@"指紋認(rèn)證失敗,%@",error.description);
                
                NSLog(@"%ld", (long)error.code); // 錯誤碼 error.code
                switch (error.code)
                {
                    case LAErrorAuthenticationFailed: // Authentication was not successful, because user failed to provide valid credentials
                    {
                        NSLog(@"授權(quán)失敗"); // -1 連續(xù)三次指紋識別錯誤
                        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"驗證結(jié)果" message:@"授權(quán)失敗" preferredStyle:UIAlertControllerStyleAlert];
                        
                        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                            
                        }];
                        [alertController addAction:cancelAction];
                        [self presentViewController:alertController animated:YES completion:nil];
                    }
                        break;
                    case LAErrorUserCancel: // Authentication was canceled by user (e.g. tapped Cancel button)
                    {
                        NSLog(@"用戶取消驗證Touch ID"); // -2 在TouchID對話框中點擊了取消按鈕
                        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"驗證結(jié)果" message:@"用戶取消驗證" preferredStyle:UIAlertControllerStyleAlert];
                        
                        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                            
                        }];
                        [alertController addAction:cancelAction];
                        [self presentViewController:alertController animated:YES completion:nil];
                        
                    }
                        break;
                    case LAErrorUserFallback: // Authentication was canceled, because the user tapped the fallback button (Enter Password)
                    {
                        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                            NSLog(@"用戶選擇輸入密碼,切換主線程處理"); // -3 在TouchID對話框中點擊了輸入密碼按鈕
                            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"驗證結(jié)果" message:@"用戶選擇輸入密碼,切換主線程處理" preferredStyle:UIAlertControllerStyleAlert];
                            
                            UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                                
                            }];
                            [alertController addAction:cancelAction];
                            [self presentViewController:alertController animated:YES completion:nil];
                        }];
                        
                    }
                        break;
                    case LAErrorSystemCancel: // Authentication was canceled by system (e.g. another application went to foreground)
                    {
                        NSLog(@"取消授權(quán),如其他應(yīng)用切入,用戶自主"); // -4 TouchID對話框被系統(tǒng)取消,例如按下Home或者電源鍵
                        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"驗證結(jié)果" message:@"取消授權(quán),如其他應(yīng)用切入,用戶自主" preferredStyle:UIAlertControllerStyleAlert];
                        
                        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                            
                        }];
                        [alertController addAction:cancelAction];
                        [self presentViewController:alertController animated:YES completion:nil];
                    }
                        break;
                    case LAErrorPasscodeNotSet: // Authentication could not start, because passcode is not set on the device.
                        
                    {
                        NSLog(@"設(shè)備系統(tǒng)未設(shè)置密碼"); // -5
                        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"驗證結(jié)果" message:@"設(shè)備系統(tǒng)未設(shè)置密碼" preferredStyle:UIAlertControllerStyleAlert];
                        
                        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                            
                        }];
                        [alertController addAction:cancelAction];
                        [self presentViewController:alertController animated:YES completion:nil];
                    }
                        break;
                    case LAErrorTouchIDNotAvailable: // Authentication could not start, because Touch ID is not available on the device
                    {
                        NSLog(@"設(shè)備未設(shè)置Touch ID"); // -6
                        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"驗證結(jié)果" message:@"設(shè)備未設(shè)置Touch ID" preferredStyle:UIAlertControllerStyleAlert];
                        
                        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                            
                        }];
                        [alertController addAction:cancelAction];
                        [self presentViewController:alertController animated:YES completion:nil];
                    }
                        break;
                    case LAErrorTouchIDNotEnrolled: // Authentication could not start, because Touch ID has no enrolled fingers
                    {
                        NSLog(@"用戶未錄入指紋"); // -7
                        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"驗證結(jié)果" message:@"用戶未錄入指紋" preferredStyle:UIAlertControllerStyleAlert];
                        
                        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                            
                        }];
                        [alertController addAction:cancelAction];
                        [self presentViewController:alertController animated:YES completion:nil];
                    }
                        break;
                        
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_9_0
                    case LAErrorTouchIDLockout: //Authentication was not successful, because there were too many failed Touch ID attempts and Touch ID is now locked. Passcode is required to unlock Touch ID, e.g. evaluating LAPolicyDeviceOwnerAuthenticationWithBiometrics will ask for passcode as a prerequisite 用戶連續(xù)多次進行Touch ID驗證失敗,Touch ID被鎖,需要用戶輸入密碼解鎖,先Touch ID驗證密碼
                    {
                        NSLog(@"Touch ID被鎖,需要用戶輸入密碼解鎖"); // -8 連續(xù)五次指紋識別錯誤,TouchID功能被鎖定,下一次需要輸入系統(tǒng)密碼
                        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"驗證結(jié)果" message:@"Touch ID被鎖,需要用戶輸入密碼解鎖" preferredStyle:UIAlertControllerStyleAlert];
                        
                        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                            
                        }];
                        [alertController addAction:cancelAction];
                        [self presentViewController:alertController animated:YES completion:nil];
                    }
                        break;
                    case LAErrorAppCancel: // Authentication was canceled by application (e.g. invalidate was called while authentication was in progress) 如突然來了電話,電話應(yīng)用進入前臺,APP被掛起啦");
                    {
                        NSLog(@"用戶不能控制情況下APP被掛起"); // -9
                    }
                        break;
                    case LAErrorInvalidContext: // LAContext passed to this call has been previously invalidated.
                    {
                        NSLog(@"LAContext傳遞給這個調(diào)用之前已經(jīng)失效"); // -10
                    }
                        break;
#else
#endif
                    default:
                    {
                        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                            NSLog(@"其他情況,切換主線程處理");
                        }];
                        break;
                    }
                }
            }
        }];
    }
    else
    {
        NSLog(@"設(shè)備不支持指紋");
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"驗證結(jié)果" message:@"設(shè)備不支持指紋" preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        [alertController addAction:cancelAction];
        [self presentViewController:alertController animated:YES completion:nil];

        NSLog(@"%ld", (long)authError.code);
        switch (authError.code)
        {
            case LAErrorTouchIDNotEnrolled:
            {
                NSLog(@"Authentication could not start, because Touch ID has no enrolled fingers");
                break;
            }
            case LAErrorPasscodeNotSet:
            {
                NSLog(@"Authentication could not start, because passcode is not set on the device");
                break;
            }
            default:
            {
                NSLog(@"TouchID not available");
                break;
            }
        }
    }

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 字典 日期,系統(tǒng)時間代碼 指紋解鎖代碼,新概念閉包的第一次應(yīng)用 函數(shù)的幾種寫法
    寫啥呢閱讀 440評論 1 0
  • 自從蘋果5S發(fā)布之后,指紋識別的功能受到了前所未有的關(guān)注和好評:方便快捷、安全高效成了它最大的優(yōu)點。不僅可以通過指...
    今日知事閱讀 545評論 0 1
  • 好久沒寫文章了,最近也比較偷懶,今天繼續(xù)討論我實際開發(fā)中遇到的需求,那就是關(guān)于APP解鎖,大家都知道?,F(xiàn)在越來越多...
    青蛙要fly閱讀 3,252評論 2 26
  • 網(wǎng)上關(guān)于指紋解鎖的教程很多了,我的處理上和他們有點區(qū)別,在這里說一下。 首先導(dǎo)入LocalAuthenticati...
    CrazySteven閱讀 555評論 0 2
  • 童話是寫給孩子的嗎? 童話是寫給未來的大人的吧。 宮崎駿的電影幾乎都是童話,有溫暖寧靜的人文意境,清新的藍(lán)天、變幻...
    天空云閱讀 197評論 0 0

友情鏈接更多精彩內(nèi)容