0

I'm converting below code to Swift 3.

 if context.canEvaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, error:nil) {

  // 2.
  context.evaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics,
    localizedReason: "Logging in with Touch ID",
    reply: { (success : Bool, error : NSError? ) -> Void in

      // 3.
      dispatch_async(dispatch_get_main_queue(), {
        if success {
          self.performSegueWithIdentifier("dismissLogin", sender: self)
        }

        if error != nil {

          var message : NSString
          var showAlert : Bool

          // 4.
          switch(error!.code) {

Step 4 does not work anymore on Xcode 8, Swift 3. So I could not do the following cases:

switch(error!.code) {
          case LAError.AuthenticationFailed.rawValue:
            message = "There was a problem verifying your identity."
            showAlert = true
            break;

Currently, it's seems there was no solution that I could find yet. Any suggestion, please let me know.

Thanks a lot!

Nirav D
  • 71,513
  • 12
  • 161
  • 183
Va Visal
  • 884
  • 1
  • 10
  • 17
  • No solution yet? The first hit for `[swift3] error code` is http://stackoverflow.com/questions/38711269/accessing-code-in-swift-3-error. – Martin R Sep 28 '16 at 09:52

2 Answers2

9

First change your reply closure of evaluatePolicy method, in Swift 3 it is Error not NSError.

reply: { (success : Bool, error : Error? ) -> Void in

Second, change performSegue with identifier like this.

performSegue(withIdentifier: "dismissLogin", sender: self)

In Swift 3 you need to convert Error object to NSError or use _code with Error instance instead of code.

switch((error! as NSError).code)

OR

switch(error!._code)

You need to also change you dispatch syntax like this.

Dispatch.async.main {
    //write code
}
Nirav D
  • 71,513
  • 12
  • 161
  • 183
2

This actually got a lot easier

context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: "Logging in with Touch ID", reply: { (success : Bool, error : Error? ) -> Void in

  // 3.
  DispatchQueue.main.async {
    if success {
      self.performSegueWithIdentifier("dismissLogin", sender: self)
    }

    if let error = error {

      var message : NSString
      var showAlert : Bool

      // 4.
      switch error {
          case LAError.userCancel:
              //do stuff

This is mostly from memory, but I think it is correct.

Bob
  • 91
  • 1
  • 5