生体認証機能を使うAndroidアプリケーション作成
プロジェクトの作成
今般は手元にあるインストールしたDelphi Community Edition12.1を使用します。
Delphiを起動し、[ファイル]⇒[新規作成]⇒[マルチデバイス アプリケーション - Delphi]をクリックします。
[空のアプリケーション]を選択して[OK]ボタンをクリックします。
実機をPCにつないでターゲットを切り替え
PCと実機のAndroidをUSBで接続します。
右の「プロジェクト」ペインにある[Android 64ビット]⇒[ターゲット]⇒[接続した実機(Pixel 7aなど)]をダブルクリックして切り替えます。
画面の設計(コンポーネントの設置)
フォームに右下ペインの「パレット」から
・TButton1
・TBiometricAuth
コンポーネントをドラッグ&ドロップして配置します。
プロパティの設定
BiometricAuth1をクリックして選択し、
「DeviceCredential」プロパティをクリックして「True」に設定します。
プロジェクトの保存
[ファイル]⇒[すべて保存]をクリックしてフォームとプロジェクトを保存します。
※注意点
フォルダ名やファイル名に全角文字が入るとコンパイルできなくなるのでCドライブ直下に任意のフォルダを作成して保存することをお勧めします。
資格リストの「生体認証サービス」の設定
[プロジェクト]⇒[オプション]をクリックします。
左ペインの[アプリケーション]⇒[資格リスト]を選択します。
「生体認証サービス」にチェックを入れて true に設定します。
[保存]ボタンを押します。
ソースコードの記述
「BiometricAuth1」をクリックして選択状態にします。
左ペイン「オブジェクトインスペクタ」で[イベント]タブをクリックして選択します。
「OnAuthenticateSuccess」の右側の何もないところをダブルクリックします。
ソースコードを記述します。
procedure TForm1.BiometricAuth1AuthenticateSuccess(Sender: TObject);
begin
ShowMessage('Success');
end;
同じように「BiometricAuth1」の「OnAuthenticateFail」イベントのソースコードを記述します。
procedure TForm1.BiometricAuth1AuthenticateFail(Sender: TObject;
const FailReason: TBiometricFailReason; const ResultMessage: string);
begin
ShowMessage('Failed');
end;
同じように「Button1」の「OnClick」イベントのソースコードを記述します。
procedure TForm1.Button1Click(Sender: TObject);
begin
if BiometricAuth1.CanAuthenticate then
begin
BiometricAuth1.GetBiometricAvailability;
BiometricAuth1.Authenticate;
end;
end;
全ソースコードは以下です。
unit Unit1;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs,
FMX.BiometricAuth, FMX.Controls.Presentation, FMX.StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
BiometricAuth1: TBiometricAuth;
procedure BiometricAuth1AuthenticateSuccess(Sender: TObject);
procedure BiometricAuth1AuthenticateFail(Sender: TObject;
const FailReason: TBiometricFailReason; const ResultMessage: string);
procedure Button1Click(Sender: TObject);
private
{ private 宣言 }
public
{ public 宣言 }
end;
var
Form1: TForm1;
implementation
{$R *.fmx}
procedure TForm1.BiometricAuth1AuthenticateFail(Sender: TObject;
const FailReason: TBiometricFailReason; const ResultMessage: string);
begin
ShowMessage('Failed');
end;
procedure TForm1.BiometricAuth1AuthenticateSuccess(Sender: TObject);
begin
ShowMessage('Success');
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
if BiometricAuth1.CanAuthenticate then
begin
BiometricAuth1.GetBiometricAvailability;
BiometricAuth1.Authenticate;
end;
end;
end.
実行する
[実行]⇒[デバッガを使わずに実行]をクリックするとコンパイルされて実機で実行されます。
[Button1]をクリックすると生体認証が起動します。
