基本 Touch ID 实施

Basic Touch ID Implementation

我一直想写一个嵌套函数,它接受 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 returns 一个闭包/函数。您调用 presentTouchID 但不以任何方式使用返回的闭包。

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

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

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

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

我不确定这在这里是否有意义。
3. 您可以从 presentTouchID
中删除布尔值作为参数 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)
    }
}