使用生物识别授权控件TBioMetricAuth
11.3带来一个新的不可视控件TBioMetricAuth,实现在Android和iOS 设备上通过生物识别技术(例如Face ID、指纹)进行授权。注意,对于Android平台,仅支持Android 10以上版本。
以下内容转组Delphi盒子,感谢作者Emailx45:How to use TBioMetricAuth component FMX in Android project by Emailx45
1) Needs 2 permissions involved: .....USE_BIOMETRIC = 11.3用这个权限 .....USE_FINGERPRINT = 小于11.3用这个权限
2) Project->Options: .....Entitlement List -> Biometric Authorization Service
.....Permissions:
..........Use Biometric (new IDE)
..........Use Fingerprint (旧版本用,新版本被Biometric取代)
或者在AndroidManifest.template.xml中直接增加
<uses-permission android:name="android.permission.USE_BIOMETRIC"/>
<uses-permission android:name="android.permission.USE_FINGERPRINT"/>
3) TBiometricAuth component: mandatory choices: .....BiometricAuth1.PromptDescription (必须设置) .....BiometricAuth1.PromptTitle (必须设置) .....BiometricAuth1.BiometricStrengths (必须设置) 4) Request/Rationale events from request permissions! 5) Biometric events if: success or failed!
附我测试的完整代码:
unit Unit16; interface uses System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants, FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, fmx.DialogService, FMX.BiometricAuth, FMX.Controls.Presentation, FMX.StdCtrls, system.Permissions; type TForm16 = class(TForm) BiometricAuth1: TBiometricAuth; Button1: TButton; procedure FormCreate(Sender: TObject); procedure Button1Click(Sender: TObject); procedure BiometricAuth1AuthenticateFail(Sender: TObject; const FailReason: TBiometricFailReason; const ResultMessage: string); procedure BiometricAuth1AuthenticateSuccess(Sender: TObject); private procedure BiometricPermissionRequestResult(Sender: TObject; const APermissions: TClassicStringDynArray; const AGrantResults: TClassicPermissionStatusDynArray); public { Public declarations } end; var Form16: TForm16; implementation {$R *.fmx} var PermissionBiometric: TArray<string>; procedure TForm16.BiometricAuth1AuthenticateFail(Sender: TObject; const FailReason: TBiometricFailReason; const ResultMessage: string); begin TDialogService.ShowMessage('Failed!'); end; procedure TForm16.BiometricAuth1AuthenticateSuccess(Sender: TObject); begin TDialogService.ShowMessage('Successed!'); end; procedure TForm16.BiometricPermissionRequestResult(Sender: TObject; const APermissions: TClassicStringDynArray; const AGrantResults: TClassicPermissionStatusDynArray); begin // 2 permissions involved: USE_BIOMETRIC / USE_FINGERPRINT if (Length(AGrantResults) = 1 {2}) and (AGrantResults[0] = TPermissionStatus.Granted) then // and (AGrantResults[1] = TPermissionStatus.Granted) then BiometricAuth1.Authenticate; //... end; procedure TForm16.Button1Click(Sender: TObject); begin PermissionsService.RequestPermissions( { } PermissionBiometric, { } BiometricPermissionRequestResult { } ); end; procedure TForm16.FormCreate(Sender: TObject); begin PermissionBiometric := ['android.permission.USE_BIOMETRIC']; // , 'android.permission.USE_FINGERPRINT']; // 不是必须设置!!! BiometricAuth1.PromptDescription := 'my description'; // 必须设置 BiometricAuth1.PromptTitle := 'my title'; // 必须设置 BiometricAuth1.BiometricStrengths := [TBiometricStrength.DeviceCredential { , others... }]; // 必须设置,指定所需的强度 end; end.