Using TouchID on Ionic 2

2019-06-14 08:00发布

问题:

I am trying to use TouchID within my Ionic 2 app and have this simplified code.

When I run the app on my iPhone I see "A" is logged in the console and then "Fingerprint or device passcode validated."
but "B" is not logged. What have I missed?

checkIn(job) {
    console.log("A");
    TouchID.verifyFingerprint('Scan your fingerprint to check in')
    .then(
      res => function() {  
        console.log("B");
      },
      err => alert('Sorry, your fingerprint is not recognised')
    );  

} 

回答1:

I am assuming you have imported Touch ID Plugin into your project using

import { TouchID } from 'ionic-native';

In your CheckIn function first check for Touch ID Availability using

TouchID.isAvailable()
.then(
  res => console.log('TouchID is available!'),
  err => console.error('TouchID is not available', err)
);

If it logs 'TouchID is available!' then in your TouchID.verifyFingerprint function log err to pinpoint the issue

TouchID.verifyFingerprint('Scan your fingerprint please')
.then(
  res => console.log('Ok', res),
  err => console.error('Error', err)
);

Error Codes

The plugin will reject for various reasons. Your app will most likely need to respond to the cases differently.

Here is a list of some of the error codes:

  • -1 - Fingerprint scan failed more than 3 times
  • -2 or -128 - User tapped the 'Cancel' button
  • -3 - User tapped the 'Enter Passcode' or 'Enter Password' button
  • -4 - The scan was cancelled by the system (Home button for example)
  • -6 - TouchID is not Available
  • -8 - TouchID is locked out from too many tries