ios - 基本 Touch ID 实现

标签 ios swift touch-id

我一直想写一个嵌套函数,它接受 touchID 的原因字符串和一个 bool 值(如果它应该显示或不显示)。这是我的代码

 import UIKit
 import LocalAuthentication  

 class XYZ : UIViewController {
     override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        presentTouchID(reasonToDsiplay: "Are you the owner?", true) //ERROR: Expression resolves to an unused function
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func presentTouchID(reasonToDsiplay reason: String, _ shouldShow: Bool) -> (Bool) -> (){

        let reason1 = reason
        let show = shouldShow

        let long1 = { (shoudlShow: Bool) -> () in

            if show{
                let car = LAContext()

                let reason = reason1

                guard car.canEvaluatePolicy(.DeviceOwnerAuthenticationWithBiometrics, error: nil) else {return}

                car.evaluatePolicy(.DeviceOwnerAuthenticationWithBiometrics, localizedReason: reason) {(success, error) in

                    guard error != nil else {return}

                    dispatch_async(dispatch_get_main_queue(), { Void in

                        print("Kwaatle")

                    })

                }

            }
            else{
                print("Mah")
            }


        }
        return long1
    }
}

当我执行 presentTouchID(reasonToDsiplay: "Are you the owner?", true)func viewDidLoad() 我收到一条错误信息

Expression resolves to an unused function.

我做错了什么?

最佳答案

问题是您的方法 presentTouchID 返回一个闭包/函数。您调用 presentTouchID 但不以任何方式使用返回的闭包。

这里有几个选项。
1.调用返回的闭包:

presentTouchID(reasonToDsiplay: "Are you the owner?", true)(true)

这看起来真的很尴尬。
2.您可以将返回的闭包存储在变量中:

let present = presentTouchID(reasonToDsiplay: "Are you the owner?", true)

我不确定这是否有意义。
3. 您可以从 presentTouchID
中删除 bool 值作为参数 4.修复返回的闭包

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    presentTouchID(reasonToDsiplay: "Are you the owner?", true) { success in
        if success {
            print("Kwaatle")
        } else {
            print("Mah")
        }
    }
}

func presentTouchID(reasonToDsiplay reason: String, _ shouldShow: Bool, completion: (evaluationSuccessfull: Bool) -> ()) {

    if shouldShow {
        let car = LAContext()

        guard car.canEvaluatePolicy(.DeviceOwnerAuthenticationWithBiometrics, error: nil) else {
            completion(evaluationSuccessfull: false)
            return
        }

        car.evaluatePolicy(.DeviceOwnerAuthenticationWithBiometrics, localizedReason: reason) {(success, error) in

            guard error != nil else {
                completion(evaluationSuccessfull: false)
                return
            }
            completion(evaluationSuccessfull: success)
        }

    } else{
        completion(evaluationSuccessfull: false)
    }
}

关于ios - 基本 Touch ID 实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37771602/

相关文章:

ios - iAd 框架在 iPhone 上返回错误的高度

swift - 以编程方式 ScrollView

ios - UITextField 密码自动填充确认

ios - iOS TouchID每次访问后如何强制重新授权,或者查看是否解锁?

ios - 在应用程序处于后台时更改状态栏中的文本

ios - 用户拒绝使用时无法检查 Face ID

ios - X秒后关闭模态ViewController

iphone - 在应用程序商店中打开 itune 链接?

ios - 使用 GCD 计算从后台队列加载数据的百分比

ios - 频繁点击按钮