Swifit 지문 인식 작은 데모

2395 단어

잔말 말고 바로 코드.

import UIKit
import LocalAuthentication

class FingerprintVerifyManager {
    //    
    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)
        }
    }
    //      
    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)")
        }
    }

좋은 웹페이지 즐겨찾기