iOS Biometric Authentication with Xamarin Forms

iOS Biometric Authentication

FaceID

TouchID

Using TouchID and FaceID in Xamarin Forms Application

<key>NSFaceIDUsageDescription</key>
<string>Use Face ID/Touch ID instead of a password to access your account
</string>
LAContext  context = new LAContext();
public enum LAPolicy : long
{
DeviceOwnerAuthenticationWithBiometrics = 1,
DeviceOwnerAuthentication = 2,
DeviceOwnerAuthenticationWithWatch = 3,
OwnerAuthenticationWithBiometricsOrWatch = 4
}
var result = await context.EvaluatePolicyAsync(LAPolicy.DeviceOwnerAuthentication, message);
[assembly: Dependency(typeof(BiometricService))]
namespace BiometricApp.iOS
{
public class BiometricService : IBiometricService
{
LAContext context = new LAContext();
public async Task<bool> LoginWithBiometrics()
{
context.LocalizedCancelTitle = "I don't want it.";
context.LocalizedFallbackTitle = "Use password.";
NSError error = new NSError();
Tuple<bool, NSError> result = new Tuple<bool, NSError>(false, null);
if (context.CanEvaluatePolicy(LAPolicy.DeviceOwnerAuthentication, out error))
{
string message = "";
switch (context.BiometryType)
{
case LABiometryType.TouchId:
message = "Put your finger on screen to be the King!";
break;
case LABiometryType.FaceId:
message = "Scan your face to be the King!";
break;
case LABiometryType.None:
message = "Your device not support you to be King!";
break;
default:
message = "We can't sure your worthiness!";
break;
}
result = await context.EvaluatePolicyAsync(LAPolicy.DeviceOwnerAuthentication, message);
}
if (result.Item1 && result.Item2 == null)
return true;
else
return false;
}
}
}
public interface IBiometricService
{
Task<bool> LoginWithBiometrics();
}
var result = await DependencyService.Get<IBiometricService>().
LoginWithBiometrics();

Alternative to Biometric Authentication

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store