Swift/iOS - Touch ID

指纹技术是苹果2013年在iPhone5s(iOS7)上开始应用的。iOS7是不允许开发人员来使用TouchAPI来验证自己的应用程序。
iOS8开始,苹果陆续开放了Touch ID公共API。TouchID的使用时“本地”的一个验证,框架给我们提供了提示用户进行身份验证的方法。我们可以使用它来认证登录,或授权访问安全敏感信息的应用程序。
我们要使用的是Local Authentication framework。
我们是直接获取TouchID来做的,很多APP都是有开关来控制,是否用TouchID来登录或者打开APP。

    // 进行验证
    func authenticateWithTouchID()
    {
        // 获取context
        let localAuthContext = LAContext.init()
        let reasonText = "使用TouchID登录"
    
        var authError: NSError?
        // 先判断是否之前不允许
        if !localAuthContext.canEvaluatePolicy(LAPolicy.deviceOwnerAuthenticationWithBiometrics, error: &authError)
        {
            if let error = authError
            {
                print(error.localizedDescription)
            }
            //  不允许就在这里弹出、推出登录界面等操作
            OperationQueue.main.addOperation({
                let alterViewController = UIAlertController.init(title: "提示",
                                                                 message: "请在这里进行错误提示",
                                                                 preferredStyle: .alert)
                let actionCancel = UIAlertAction.init(title: "取消",
                                                      style: .cancel,
                                                      handler: { (action:UIAlertAction) in})
                alterViewController.addAction(actionCancel)
                self.present(alterViewController, animated: true, completion: nil)
            })
            
            return
        }
        
        localAuthContext.evaluatePolicy(LAPolicy.deviceOwnerAuthenticationWithBiometrics,
                                        localizedReason: reasonText,
                                        reply: {(success:Bool, error:Error?) -> Void in
            if !success
            {
                if let error = error {
                    switch error {
                    case LAError.authenticationFailed:
                        print("授权失败")
                    case LAError.passcodeNotSet:
                        print("没有设置")
                    case LAError.systemCancel:
                        print("验证被系统取消")
                    case LAError.userCancel:
                        print("验证被用户取消")
                    case LAError.touchIDNotEnrolled:
                        print("验证无法开启,没有登记的手指认证ID")
                    case LAError.touchIDNotAvailable:
                        print("验证无法开启,TouchID不可用")
                    case LAError.userFallback:
                        // 用户点击了取消按钮,要进行登录/输入密码等操作
                        // 在错误列表都应该输入密码来进行操作
                        // 点击输入密码也是进入这里
                        print("用户取消了操作")
                    default:
                        print(error.localizedDescription)
                    }
                }
                // 失败在这里弹出、推出登录界面
                OperationQueue.main.addOperation({
                    let alterViewController = UIAlertController.init(title: "提示",
                                                                     message: "获取失败/错误提示",
                                                                     preferredStyle: .alert)
                    let actionCancel = UIAlertAction.init(title: "取消",
                                                          style: .cancel,
                                                          handler: { (action:UIAlertAction) in})
                    alterViewController.addAction(actionCancel)
                    
                    self.present(alterViewController, animated: true, completion: nil)
                })
            }
            else
            {
                // 允许并且成功就在这里进行操作
                OperationQueue.main.addOperation({
                    let alterViewController = UIAlertController.init(title: "提示",
                                                                     message: "成功就在这里进行操作",
                                                                     preferredStyle: .alert)
                    let actionCancel = UIAlertAction.init(title: "取消",
                                                          style: .cancel,
                                                          handler: { (action:UIAlertAction) in})
                    alterViewController.addAction(actionCancel)
                    
                    self.present(alterViewController, animated: true, completion: nil)
                })
            }
        })
    }
Swift/iOS - Touch ID_第1张图片
195D45996A3B63E3C4AD4EC0E7D2E1DD.png

这里是苹果的pdf文档:iOS_Security_Guide_Oct_2014

你可能感兴趣的:(Swift/iOS - Touch ID)