Swifit指紋識別小Demo

廢話不說直接上代碼

import UIKit
import LocalAuthentication

class FingerprintVerifyManager {
    //單例實現(xiàn)
    static let instance = FingerprintVerifyManager()
    private init(){}
    //驗證完的閉包回調
    typealias TouchIdVerify = (isSuccess:Bool, error:NSError?) ->()
    //調用指紋驗證
    func touchIdWithHand(identtyVerify:TouchIdVerify) {
        
        let version = UIDevice.currentDevice().systemVersion
        
        let result = version.compare("8.0.0")
        
        assert(result == NSComparisonResult.OrderedDescending, "IOS8.0以上可使用")
        
        let context = LAContext()
        
        let resultMsg = "驗證指紋密碼"
        //設備驗證
        let (deviceVerify, error) = checkIsOpenFingerprintVerify()
        if deviceVerify {
            context.evaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, localizedReason: resultMsg, reply: { (isSuccess, error) -> Void in
                NSOperationQueue.mainQueue().addOperationWithBlock({ () -> Void in
                    identtyVerify(isSuccess: isSuccess, error: error)
                })
            })
        }else {
            print("失敗\(error!.code)")
            deviceVerifyWithError(error)
        }
    }
    //驗證出現(xiàn)錯誤
    func deviceVerifyWithError(error:NSError!) {
        switch error!.code {
        case Int(kLAErrorTouchIDNotEnrolled):
            print("\(kLAErrorTouchIDNotEnrolled)")
            print("設備支持,但用戶沒有設置")
            break;
        case Int(kLAErrorPasscodeNotSet):
            print("\(kLAErrorPasscodeNotSet)")
            print("設備支持,但是被禁用")
            break;
        default:
            print("設備不支持")
            break;
        }
    }
    //設備是否打開/支持指紋驗證
    func checkIsOpenFingerprintVerify() -> (isopen:Bool, error:NSError?) {
        let context = LAContext()
        var error:NSError?
        let isOpen = context.canEvaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, error: &error)
        return (isOpen, error)
    }
}

調用

    @IBAction func fingerprintVerify(sender: AnyObject) {
        
        let finerVerify = FingerprintVerifyManager.instance
        
         finerVerify.touchIdWithHand { (isSuccess, error) -> () in
            print("success:\(isSuccess), error:\(error?.code)")
        }
    }
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,030評論 25 709
  • 發(fā)現(xiàn) 關注 消息 iOS 第三方庫、插件、知名博客總結 作者大灰狼的小綿羊哥哥關注 2017.06.26 09:4...
    肇東周閱讀 15,296評論 4 61
  • 轆轤打水,小氣候村里沒有自來水,都是從井里把水用桶提上來。提水太費勁,人們就湊錢請人來做一臺轆轤,放在井上。搖動把...
    劉現(xiàn)輝民俗畫閱讀 992評論 0 0
  • 人生于世,各有不同的愛憎與訴求,本是不可一概而論的。受限于他本人的認知、條件及經驗,能滿足他的東西亦各不相同,而他...
    更向遠行閱讀 481評論 0 0

友情鏈接更多精彩內容