有没有办法接受 Touch ID,而不触发默认的 MacOS Touch ID 提示?或者有没有办法大量定制默认的 Touch ID 提示?
我见过 1Password 这样做过,所以这是可能的。
我尝试按照这些教程进行操作,但没有成功
使用 Face ID 或 Touch ID 将用户登录到您的应用程序
我也读过有关 Passkeys 的内容,但我认为它不会起作用,因为它用于 Web 身份验证。
任何正确方向的指示将不胜感激!
这是 1Password 如何执行此操作的屏幕截图:
您尝试做的事情不受支持,至少官方不支持。 您可以尝试使用自定义 UI 覆盖。代码位于 swift。
import SwiftUI
import LocalAuthentication
struct ContentView: View {
@State private var authenticationResult: AuthenticationResult?
var body: some View {
VStack {
Text("Touch ID Authentication (add your custom text here)")
.padding()
Button("Authenticate with Touch ID") {
authenticateWithTouchID()
}
.padding()
if let result = authenticationResult {
CustomResultView(result: result)
.padding()
}
}
}
func authenticateWithTouchID() {
let context = LAContext()
if context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: nil) {
context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: "Authenticate using Touch ID") { success, error in
DispatchQueue.main.async {
if success {
authenticationResult = .success
} else {
authenticationResult = .failure
}
}
}
} else {
authenticationResult = .notAvailable
}
}
}
struct CustomResultView: View {
let result: AuthenticationResult
var body: some View {
VStack {
switch result {
case .success:
Image(systemName: "checkmark.circle")
.foregroundColor(.green)
.font(.system(size: 40))
Text("Authentication Successful")
.foregroundColor(.green)
case .failure:
Image(systemName: "xmark.circle")
.foregroundColor(.red)
.font(.system(size: 40))
Text("Authentication Failed")
.foregroundColor(.red)
case .notAvailable:
Image(systemName: "exclamationmark.circle")
.foregroundColor(.orange)
.font(.system(size: 40))
Text("Touch ID not available or not configured")
.foregroundColor(.orange)
}
}
}
}
enum AuthenticationResult {
case success
case failure
case notAvailable
}
@main
struct TouchIDExampleApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}