Touch ID with Cordova / PhoneGap

iPhone fingerprint recognition

In August 2013 (before Touch ID or the iPhone 5s were released) I wrote a blog post guessing that the hardware and software capabilities of fingerprint recognition could be just around the corner and might become available to app developers.

Apple, to much disappointment, kept the API private with iOS 7 but this has all changed with the (beta) release of iOS 8. Now, you got the touch

The local authentication framework

The moment Xcode 6 beta had finished downloading, the first thing I did was create a dummy project with the newly available “Local Authentication” framework. It took me quite some time to realise that adding the reference (either #import <LocalAuthentication/LocalAuthentication.h> or as a module @import LocalAuthentication) broke the build if you don’t target a real device (learn from me here and save yourself some time!).

Cordova / PhoneGap plugin

The next logical step for me was to create a plugin from my test code, so that I could launch Touch ID authentication right from within my JavaScript.

The plugin is now in beta release (as is the API and subject to change etc). I have added this to plugreg and you can also view the full source code on Github.

Add the plugin with the CLI

1
cordova plugin add https://github.com/leecrossley/cordova-plugin-touchid.git

Using the plugin

Ensure you use the plugin after your deviceready event has been fired and you are targeting a real device.

touchid.authenticate()

Pass the following arguments to the authenticate() function, to prompt the user to authenticate via TouchID:

  1. Success callback (called on successful authentication)
  2. Failure callback (called on error or if authentication fails)
  3. Localised text explaining why the app needs authentication. This should be localised and provide a clear reason for why you are requesting that the user performs the authentication, and what action you will be taking.
1
window.touchid.authenticate(successCallback, failureCallback, "text");

touchid.checkSupport()

Although the authenticate() function will return an error if the user is unable to authenticate via Touch ID, you may wish to check support without prompting the user to authenticate. This can be done by passing following arguments to the checkSupport() function:

  1. Success callback (called if authentication is possible)
  2. Not supported callback (called if policy can not be evaluated, with error message)
1
window.touchid.checkSupport(successCallback, notSupportedCallback);

Improvements?

Probably. Issues and pull request on the git repo please. Remember this is still beta.