Dershowitz123 Dershowitz123 - 5 months ago 20
iOS Question

Basic Touch ID Implementation

I've been meaning to write a nested function which accepts the reason string for touchID and a bool value if it should be shown or not. This is my code

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
}
}


When I do
presentTouchID(reasonToDsiplay: "Are you the owner?", true)
in
func viewDidLoad()
I get an error saying


Expression resolves to an unused function.


What am I doing wrong?

Answer

The problem is that your method presentTouchID returns a closure / function. You call presentTouchID but do not use the returned closure in any way.

You have a few options here.
1. Call the returned closure:

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

which looks really really awkward.
2. You can store the returned closure in a variable:

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

I am not sure though if that makes any sense here.
3. You can remove the boolean as argument from the presentTouchID
4. OR fix the returned closure

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)
    }
}
Comments