LocalAuthentication inside iMessage Extensions

Did anybody have any luck with using LocalAuthentication from inside an iMessage extension?


In my case I'm trying to verify TouchID before showing details of a custom message in the expanded presenation style.


Here is what I do:


override func willBecomeActive(with conversation: MSConversation) {
    guard let _ = conversation.selectedMessage else {
// no selected message show compose
        presentViewController(for: conversation, with: presentationStyle)
        return
    }
  
// we need to show details of a selected message
    self.authenticate { (error) in
        guard error == nil else {
            print(error)
            return
        }
        self.presentViewController(for: conversation, with: .expanded)
    }
}

func authenticate(completion: (NSError?)->()) {
    let myContext = LAContext()
    let myLocalizedReasonString = "Use TouchID to view details"
   
    var authError: NSError? = nil
    if myContext.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &authError) {
        myContext.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: myLocalizedReasonString) { (success, evaluateError) in
            if (success) {
                completion(nil)
            } else {
                completion(evaluateError)
            }
        }
    } else {
        completion(authError)
    }
}


When selecting a message to show details, the expanded mode appears before willBecomeActive() is called. Then the TouchID alert is presented but this causes the empty expanded message view to disappear. The completion block of authenticate() is not called regardless of if TouchID was successful or not.


When the user selects another message, the extension calls the completion handler from the previous try before calling authenticate() again. Unsurprisingly this causes a lot to break. Only way to get out of this loop is to kill Messages.app and restart.


Any ideas? Am I doing something wrong or is this intended functionality?

Accepted Reply

Beta 4 is now available, it would be great to get people to test the LocalAuthentication and TouchID support with it. Please post back with results.

Replies

When the TouchID alert is replaced with a dummy alert view it works as expected:


func authenticate(completion: (NSError?)->()) {
    let alert = UIAlertController(title: "Touch ID", message: "Pretend we had TouchID here.", preferredStyle: .alert)
    let action = UIAlertAction(title: "Let me see!", style: .default) { (action) in
        completion(nil)
    }
    alert.addAction(action)
    self.present(alert, animated: true, completion: nil)
}


Is this maybe a bug in how the Touch ID alert gets presented?

I am having the same issue trying to implement TouchID into my iMessage app and am curious about this as well.

I've submitted a radar: 27473215

Yup, same issue here as well.

I also filed a radar.

Any response?

The LocalAuthentication APIs are not working in iMessage apps in Beta 3. This should be resolved in the next beta release, so please try it again there.

Post not yet marked as solved Up vote reply of pdm Down vote reply of pdm

Beta 4 is now available, it would be great to get people to test the LocalAuthentication and TouchID support with it. Please post back with results.

I'm developing a keyboard extension with Touch Id authentication and it still happens to in iMessage .

it happens on search bar and on create new message screen.

I can see that UIInputViewController's "viewWillDissapear" and "viewDidDisappear" are getting called once this pop up appears.

is there some work around for this issue ?

I would recommend getting a new bug report filed with a sample project showing the issue that you're seeing. It's possible you're seeing a different problem so we'll need to investigate it.