This project provides a easy way to call biometric authentication (Face / Fingerprint) function in Xamarin Forms
- Add Support to Android 12
Install-Package Plugin.XF.TouchID
- In AppDelegate.cs
global::Xamarin.Forms.Forms.Init();
LoadApplication(new App());
//Init the library
Plugin.XF.TouchID.TouchID.Init();
return base.FinishedLaunching(app, options);
- In your info.plist, add face id permission request
<key>NSFaceIDUsageDescription</key>
<string>Need your face to unlock secrets!</string>
FaceID | TouchID |
---|---|
-
Set TargetFramework as Android10.0 (Q) API Level 29
-
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);
}
- 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+ |
---|---|
// Support = 0,
// DeviceNotSecured = 1,
// NotEnrolledFinger = 2,
// HardwareNotSupport = 3,
// OSVersionNotSupport = 4,
Plugin.XF.TouchID.TouchIDStatus possible = Plugin.XF.TouchID.TouchID.IsFingerprintAuthenticationPossible());
Plugin.XF.TouchID.TouchID.PromptSecuritySettings();
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);
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);