はじめに
-
パッケージをインストール
Terminal window npm i @capgo/capacitor-native-biometricTerminal window pnpm add @capgo/capacitor-native-biometricTerminal window yarn add @capgo/capacitor-native-biometricTerminal window bun add @capgo/capacitor-native-biometric -
ネイティブプロジェクトと同期
Terminal window npx cap syncTerminal window pnpm cap syncTerminal window yarn cap syncTerminal window bunx cap sync -
権限を設定
Info.plistにFace ID使用説明を追加します:<key>NSFaceIDUsageDescription</key><string>For secure authentication</string>Android
Section titled “Android”AndroidManifest.xmlに生体認証の権限を追加します:<uses-permission android:name="android.permission.USE_BIOMETRIC" />
プラグインをインポートし、そのメソッドを生体認証に使用します:
import { NativeBiometric } from '@capgo/capacitor-native-biometric';
// 生体認証が利用可能か確認const checkBiometric = async () => { const result = await NativeBiometric.isAvailable();
if (result.isAvailable) { console.log(`生体認証タイプ: ${result.biometryType}`); }};
// 生体認証を実行const verify = async () => { const verified = await NativeBiometric.verifyIdentity({ reason: "アカウントへの安全なアクセスのため", title: "認証", subtitle: "本人確認", description: "センサーに指を置いてください" }) .then(() => true) .catch(() => false);
if (verified) { console.log("認証成功"); }};
// 認証情報を安全に保存const saveCredentials = async () => { await NativeBiometric.setCredentials({ username: "user@example.com", password: "securepassword", server: "https://api.example.com" });};
// 保存された認証情報を取得const getCredentials = async () => { const credentials = await NativeBiometric.getCredentials({ server: "https://api.example.com" });
console.log(credentials.username); console.log(credentials.password);};
// 保存された認証情報を削除const deleteCredentials = async () => { await NativeBiometric.deleteCredentials({ server: "https://api.example.com" });};APIリファレンス
Section titled “APIリファレンス”isAvailable()
Section titled “isAvailable()”デバイスで生体認証が利用可能かどうかを確認します。
interface AvailableResult { isAvailable: boolean; biometryType?: BiometryType; errorCode?: number;}
enum BiometryType { NONE = 0, TOUCH_ID = 1, FACE_ID = 2, FINGERPRINT = 3, FACE_AUTHENTICATION = 4, IRIS_AUTHENTICATION = 5}verifyIdentity(options)
Section titled “verifyIdentity(options)”生体認証を実行します。
interface BiometricOptions { reason?: string; title?: string; subtitle?: string; description?: string; fallbackTitle?: string; useFallback?: boolean; maxAttempts?: number;}setCredentials(options)
Section titled “setCredentials(options)”生体認証保護を使用して認証情報を安全に保存します。
interface Credentials { username: string; password: string; server: string;}getCredentials(options)
Section titled “getCredentials(options)”生体認証後に保存された認証情報を取得します。
interface GetCredentialOptions { server: string;}deleteCredentials(options)
Section titled “deleteCredentials(options)”保存された認証情報を削除します。
interface DeleteCredentialOptions { server: string;}ベストプラクティス
Section titled “ベストプラクティス”-
常に最初に可用性を確認
const result = await NativeBiometric.isAvailable();if (!result.isAvailable) {// パスワード認証にフォールバック} -
明確な理由を提供 ユーザーの信頼を構築するために、生体認証が必要な理由を常に説明します。
-
エラーを適切に処理
try {await NativeBiometric.verifyIdentity({reason: "安全なデータにアクセス"});} catch (error) {// キャンセルまたは失敗を処理console.log("認証に失敗またはキャンセルされました");} -
適切なフォールバックを使用 生体認証が失敗する可能性のあるデバイスのために、パスコードフォールバックを有効にします。
プラットフォームの違い
Section titled “プラットフォームの違い”- Touch IDとFace IDをサポート
- Face IDには
NSFaceIDUsageDescriptionが必要 - デバイスパスコードへのフォールバックが利用可能
Android
Section titled “Android”- 指紋、顔、虹彩認識をサポート
USE_BIOMETRIC権限が必要- Android 9以降用のBiometric Prompt API