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)
                })
            }
        })
    }
195D45996A3B63E3C4AD4EC0E7D2E1DD.png

这里是苹果的pdf文档:iOS_Security_Guide_Oct_2014

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 阳光穿过厚厚的云层,照耀在鲁西鲁本家的庄园上,在一片葱郁的树木包围下,沾露的杂交茶香月季静静绽放,等待着主人的临幸...
    柳绮湘言阅读 462评论 0 4
  • 学经日期:2017年10月16日 星期一 雨 宝贝年龄:女儿六岁七个月,儿子两个半月 学经时间:2年10个月 学经...
    苏良蓉阅读 528评论 0 1
  • 文 简以兮 “纵浪大化中,不喜亦不惧。应尽便须尽,无复独多愁” ——陶渊明 这是季羡林先生的座右铭,如今也成...
    简以兮阅读 740评论 0 1