Skip to content

Latest commit

 

History

History
160 lines (142 loc) · 9.29 KB

README.md

File metadata and controls

160 lines (142 loc) · 9.29 KB

Plugin.XF.TouchID

This project provides a easy way to call biometric authentication (Face / Fingerprint) function in Xamarin Forms

Support

Android 6 - 12 TouchID (Target API Level 31)

iOS 10+ FaceID and TouchID

Release notes

Version 2.2

  1. Add Support to Android 12

Nuget installation

Install to your Xamarin Project

Install-Package Plugin.XF.TouchID

Build Status

Android Build status
iOS

iOS Guide

  1. In AppDelegate.cs
   global::Xamarin.Forms.Forms.Init();
   LoadApplication(new App());
   //Init the library
   Plugin.XF.TouchID.TouchID.Init();
   return base.FinishedLaunching(app, options);
  1. In your info.plist, add face id permission request
<key>NSFaceIDUsageDescription</key>
<string>Need your face to unlock secrets!</string>
FaceID TouchID

Android Guide

  1. Set TargetFramework as Android10.0 (Q) API Level 29

  2. In MainActivity

   global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
   Xamarin.Essentials.Platform.Init(this, savedInstanceState);
   //Init the library
   //Use secret key, please use a unique keyname
   Plugin.XF.TouchID.TouchID.Init(this, "plugin.xf.touchid.fingerprintkey");
   //If you do not want to use secret key
   //Plugin.XF.TouchID.TouchID.Init(this);
   LoadApplication(new App());
   protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
   {
        Plugin.XF.TouchID.TouchID.OnKeyguardManagerResult(data, requestCode, resultCode);
        base.OnActivityResult(requestCode, resultCode, data);
   }
  1. Manifest.xml add Fingerprint permission
<uses-permission android:name="android.permission.USE_FINGERPRINT" />
<!--Android 9+-->
<uses-permission android:name="android.permission.USE_BIOMETRIC"/>
Android 6-8 Android 9+

Use in Xamarin Forms

Check the device availabilities

//    Support = 0,
//    DeviceNotSecured = 1,
//    NotEnrolledFinger = 2,
//    HardwareNotSupport = 3,
//    OSVersionNotSupport = 4,
   Plugin.XF.TouchID.TouchIDStatus possible = Plugin.XF.TouchID.TouchID.IsFingerprintAuthenticationPossible());

Prompt Security page for user to enroll finger or add passcode

   Plugin.XF.TouchID.TouchID.PromptSecuritySettings();

Do the authentication

Use passcode / pin for alternative authentication (Android only, iOS default allowed)
	var dialogConfig = new Plugin.XF.TouchID.DialogConfiguration(dialogTitle: "Sign In", //Display in Android only
                                                                         dialogDescritpion: "Detect you biometic to auth", //Display on Android and iOS(TouchID)
                                                                         successAction: () =>
                                                                         {
                                                                             //Will fired when authentication success
                                                                             Device.BeginInvokeOnMainThread(() =>
                                                                             {
                                                                                 DisplayAlert("Congratulation", "You pass the authentication", "OK");
                                                                             });
                                                                         },
                                                                         alterAuthButtonText: "Use PIN", //Display in Android only
                                                                         fingerprintDialogConfiguration: new Plugin.XF.TouchID.FingerprintDialogConfiguration
                                                                         {
                                                                             //For Android 6-8 only
                                                                             FingerprintHintString = "Touch Sensor",
                                                                             FingerprintNotRecoginzedString = "Not regonized"
                                                                         },
                                                                         failedAction: () =>
                                                                         {  
                                                                             //For Android 6-8 only
                                                                             Device.BeginInvokeOnMainThread(() =>
                                                                             {
                                                                                 DisplayAlert("Alert", "Too many unsuccessful attempt, please try again later", "OK");
                                                                             });
                                                                         });

   await Plugin.XF.TouchID.TouchID.Authenticate(dialogConfig);
Use customized action as alternative (Android only, iOS default use password)
  var dialogConfig = new Plugin.XF.TouchID.DialogConfiguration(dialogTitle: "Sign In", //Display in Android only
                                                                         dialogDescritpion: "Detect you biometic to auth", //Display on Android and iOS(TouchID)
                                                                         successAction: () =>
                                                                         {
                                                                             //Will fired when authentication success
                                                                             Device.BeginInvokeOnMainThread(() =>
                                                                             {
                                                                                 DisplayAlert("Congratulation", "You pass the authentication", "OK");
                                                                             });
                                                                         },
                                                                         customizedAction: new Plugin.XF.TouchID.CustomizedAction("Cancel", () =>
                                                                         {
                                                                             //Android Only
                                                                             Device.BeginInvokeOnMainThread(() =>
                                                                             {
                                                                                 DisplayAlert("Alert", "You cancel the authentication", "OK");
                                                                             });
                                                                         }),
                                                                         fingerprintDialogConfiguration: new Plugin.XF.TouchID.FingerprintDialogConfiguration
                                                                         {
                                                                             //For Android 6-8 only
                                                                             FingerprintHintString = "Touch Sensor",
                                                                             FingerprintNotRecoginzedString = "Not regonized"
                                                                         },
                                                                         failedAction: () =>
                                                                         {
                                                                             //For Android 6-8 only
                                                                             Device.BeginInvokeOnMainThread(() =>
                                                                             {
                                                                                 DisplayAlert("Alert", "Too many unsuccessful attempt, please try again later", "OK");
                                                                             });
                                                                         });
	await Plugin.XF.TouchID.TouchID.Authenticate(dialogConfig);