簡體   English   中英

首次成功驗證后未出現生物識別提示

[英]Biometrics prompt not appearing after first successful authentication

我在下面有一個工作示例,但有點解釋。

我希望用戶能夠切換選項以使用生物識別技術解鎖他們的應用數據(如果願意,也可以不解鎖)。 如果他們激活了切換,一旦應用程序退出后台或在下次啟動時被終止,他們應該被提示登錄。

這部分應用程序功能我已經運行。 但是,一旦用戶登錄一次,就會退出后台,然后立即重新啟動他們所在的位置。

我更改了代碼庫,以便將“權限”布爾設置為 false,但是當驗證視圖提示它們時,沒有任何 Apple 生物特征,它們只是被授予訪問權限。

我嘗試使用LAContext.invalidate但在退出后台時將其添加到檢查中后,生物識別提示永遠不會再次出現 - 除非完全終止。

我是否遺漏了什么,或者銀行等其他應用程序如何在每個前台實例上創建提示?

// main.swift
@main
struct MyApp: App {
  @StateObject var biometricsVM = BiometricsViewModel()
  var body: some Scene {
    WindowGroup {
      // toggle for use
      if UserDefaults.shared.bool(forKey: .settingsBiometrics) {
        // app unlocked
        if biometricsVM.authorisationGranted {
          MyView() // <-- the app view itself
           .onAppear {
             NotificationCenter.default.addObserver(
               forName: UIApplication.willResignActiveNotification,
               object: nil,
               queue: .main
             ) { _ in
               biometricsVM.context.invalidate()
               biometricsVM.authorisationGranted = false
             }
           }
        } else {
          BioCheck(vm: biometricsVM)
        }
      }
    }
  }
}
// biometricsVM.swift
final class BiometricsViewModel: ObservableObject {
  @Published var authorisationGranted = false
  @Published var authorisationError: Error?

  let context = LAContext()

  func requestAuthorisation() {
    var error: NSError? = nil
    let hasBiometricsEnabled = context.canEvaluatePolicy(
      .deviceOwnerAuthentication, error: &error
    )

    let reason = "Unlock to gain access to your data"

    if hasBiometricsEnabled {
      switch context.biometryType {
        case .touchID, .faceID:
          context.evaluatePolicy(.deviceOwnerAuthentication, localizedReason: reason ) { success, error in
            DispatchQueue.main.async {
              self.authorisationGranted = success
              self.authorisationError = error
            }
          }

         case .none:
          // other stuff 

        @unknown default:
          // other stuff 
      }
    }
  }
}
// biocheck.swift
struct BioCheck: View {
  @ObservedObject var vm: BiometricsViewModel
  var body: some View {
    Button {
     vm.requestAuthorisation()
    } label: {
     Text("Authenticate")
    }
    .onAppear { vm.requestAuthorisation() }
  }
}

問題視頻:

問題是MyApp中的代碼在應用程序打開后運行,類似於didFinishLaunchingWithOptions 要解決此問題,請創建一個新View並將以下代碼放入其中:

if UserDefaults.shared.bool(forKey: .settingsBiometrics) {
    if biometricsVM.authorisationGranted {
        MyView()
            .onAppear {
                NotificationCenter.default.addObserver(
                    forName: UIApplication.willResignActiveNotification,
                    object: nil,
                    queue: .main
                ) { _ in
                    biometricsVM.context.invalidate()
                    biometricsVM.authorisationGranted = false
                }
            }
    } else {
        BioCheck(vm: biometricsVM)
    }
}

然后將WindowGroup的內容替換為您創建的View

編輯:

是函數requestAuthorisation給出了與context相關的錯誤。 每次調用該函數時都應該創建一個新的上下文:

  func requestAuthorisation() {
    var error: NSError? = nil
    let context = LAContext()
    let hasBiometricsEnabled = context.canEvaluatePolicy(
        .deviceOwnerAuthentication, error: &error
    )
    
    let reason = "Unlock to gain access to your data"
    
    if hasBiometricsEnabled {
        switch context.biometryType {
        case .touchID, .faceID:
            context.evaluatePolicy(.deviceOwnerAuthentication, localizedReason: reason ) { success, error in
                DispatchQueue.main.async {
                    self.authorisationGranted = success
                    self.authorisationError = error
                }
            }
            
        case .none:
            // other stuff
            break
        @unknown default:
            break
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM