【Xcode/SwiftUI】生体認証の実装 (FaceID)

実装

Info.plistへの追記事項

これを追加する (Valueは日本語でもOK)

Privacy - Face ID Usage Description: We need to unlock your data

コーディング

import SwiftUI
import LocalAuthentication

struct HomeView: View {
    @State private var isUnlocked = false

    var body: some View {
        VStack(spacing: 40) {
            if isUnlocked {
                Text("App Unlocked")
                    .font(.largeTitle)
                Image(systemName: "lock.open")
                    .resizable()
                    .frame(width: 100, height: 140)
            } else {
                Text("App Locked")
                    .font(.largeTitle)
                Image(systemName: "lock.fill")
                    .resizable()
                    .frame(width: 100, height: 140)
            }
        }
        .onAppear(perform: authenticate)
    }

    private func authenticate() {
        let context = LAContext()
        var error: NSError?
        if context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error) {
            let reason = "We need to unlock your data."
            context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: reason) { success, authenticationError in
                if success {
                    isUnlocked = true
                } else {
                    print("DEBUG_PRINT: Authentication Failed!")
                }
            }
        } else {
            print("DEBUG_PRINT: No Biometrics!")
        }
    }
}

この記事は役に立ちましたか?

はい
いいえ
貴重なフィードバックありがとうございます!