iOS - Face ID 生物识别集成
iOS - Face ID biometric integration
我的应用程序已经 integrated/implemented 面容 ID(本地身份验证)身份验证并且一切正常,除了面容 ID 提示警报 window 界面。
It shows, a rounded square with a light gray background and the title "Face
ID".
标题正上方的空白区域需要设置什么? space 是人脸 ID 图标吗?如果是,那我该如何设置呢?我已经尝试了 LAContext 和 LAPolicy 中的所有内容。
看看这个快照:
这是我的代码:
let laContext = LAContext()
var error: NSError?
let biometricsPolicy = LAPolicy.deviceOwnerAuthenticationWithBiometrics
if (laContext.canEvaluatePolicy(biometricsPolicy, error: &error)) {
if let laError = error {
print("laError - \(laError)")
return
}
var localizedReason = "Unlock device"
if #available(iOS 11.0, *) {
switch laContext.biometryType {
case .faceID: localizedReason = "Unlock using Face ID"; print("FaceId support")
case .touchID: localizedReason = "Unlock using Touch ID"; print("TouchId support")
case .none: print("No Biometric support")
}
} else {
// Fallback on earlier versions
}
laContext.evaluatePolicy(biometricsPolicy, localizedReason: localizedReason, reply: { (isSuccess, error) in
DispatchQueue.main.async(execute: {
if let laError = error {
print("laError - \(laError)")
} else {
if isSuccess {
print("sucess")
} else {
print("failure")
}
}
})
})
}
这只发生在模拟器中,在实际设备中 canvas 被面部图标动画占用。
localizedReason 仅适用于 Touch ID,因为它们共享相同的 API。
更新1:添加屏幕录像:
- iPhone X: https://youtu.be/lklRnLNHyQk
- iPhone 7: https://youtu.be/iIcduvD5JO0
- iPhone X 模拟器:https://youtu.be/bOlRVLIND5c
他们都运行相同的代码:
func beginFaceID() {
guard #available(iOS 8.0, *) else {
return print("Not supported")
}
let context = LAContext()
var error: NSError?
guard context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error) else {
return print(error)
}
let reason = "Face ID authentication"
context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: reason) { isAuthorized, error in
guard isAuthorized == true else {
return print(error)
}
print("success")
}
}
这是包含所有错误代码的 TouchID 和 FaceID 的工作代码 (Swift 4)
在任何使用生物识别技术的项目中,将 NSFaceIDUsageDescription 密钥包含在您应用的 Info.plist
文件中。如果没有此密钥,系统将不允许您的应用使用面容 ID。
let authContext = LAContext()
authContext.localizedFallbackTitle = "Use Passcode"
authContext.localizedCancelTitle = "Cancel"
var authError: NSError?
if authContext.canEvaluatePolicy(.deviceOwnerAuthentication, error: &authError) {
evaluatePolicy(policy, context: authContext)
} else {
guard let error = authError else { return }
print("Error: \(error.code)")
checkError(error)
}
private func evaluatePolicy(_ policy: LAPolicy, context: LAContext) {
context.evaluatePolicy(policy, localizedReason: reason) { (success, error) in
if success {
print("Success")
} else {
guard let error = error else { return }
self.checkError(error as NSError)
}
}
}
private func checkError(_ error: NSError) {
guard let error = error as? LAError else { return }
switch error.code {
case .authenticationFailed:
print("authenticationFailed")
requestAuth(policy: .deviceOwnerAuthentication)
case .userFallback:
print("userFallback")
requestAuth(policy: .deviceOwnerAuthentication)
case .userCancel:
print("userCancel")
case .systemCancel:
print("systemCancel")
case .passcodeNotSet:
print("passcodeNotSet")
case .appCancel:
print("appCancel")
case .invalidContext:
print("invalidContext")
case .notInteractive:
print("notInteractive")
default:
checkBioMetricError(error)
}
}
private func checkBioMetricError(_ error: LAError) {
if #available(iOS 11.0, *) {
switch error.code {
case .biometryNotAvailable,
.biometryNotEnrolled,
.biometryLockout:
requestAuth(policy: .deviceOwnerAuthentication)
default: break
}
} else {
switch error.code {
case .touchIDNotAvailable,
.touchIDNotEnrolled,
.touchIDLockout:
requestAuth(policy: .deviceOwnerAuthentication)
default: break
}
}
}
我的应用程序已经 integrated/implemented 面容 ID(本地身份验证)身份验证并且一切正常,除了面容 ID 提示警报 window 界面。
It shows, a rounded square with a light gray background and the title "Face ID".
标题正上方的空白区域需要设置什么? space 是人脸 ID 图标吗?如果是,那我该如何设置呢?我已经尝试了 LAContext 和 LAPolicy 中的所有内容。
看看这个快照:
这是我的代码:
let laContext = LAContext()
var error: NSError?
let biometricsPolicy = LAPolicy.deviceOwnerAuthenticationWithBiometrics
if (laContext.canEvaluatePolicy(biometricsPolicy, error: &error)) {
if let laError = error {
print("laError - \(laError)")
return
}
var localizedReason = "Unlock device"
if #available(iOS 11.0, *) {
switch laContext.biometryType {
case .faceID: localizedReason = "Unlock using Face ID"; print("FaceId support")
case .touchID: localizedReason = "Unlock using Touch ID"; print("TouchId support")
case .none: print("No Biometric support")
}
} else {
// Fallback on earlier versions
}
laContext.evaluatePolicy(biometricsPolicy, localizedReason: localizedReason, reply: { (isSuccess, error) in
DispatchQueue.main.async(execute: {
if let laError = error {
print("laError - \(laError)")
} else {
if isSuccess {
print("sucess")
} else {
print("failure")
}
}
})
})
}
这只发生在模拟器中,在实际设备中 canvas 被面部图标动画占用。
localizedReason 仅适用于 Touch ID,因为它们共享相同的 API。
更新1:添加屏幕录像:
- iPhone X: https://youtu.be/lklRnLNHyQk
- iPhone 7: https://youtu.be/iIcduvD5JO0
- iPhone X 模拟器:https://youtu.be/bOlRVLIND5c
他们都运行相同的代码:
func beginFaceID() {
guard #available(iOS 8.0, *) else {
return print("Not supported")
}
let context = LAContext()
var error: NSError?
guard context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error) else {
return print(error)
}
let reason = "Face ID authentication"
context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: reason) { isAuthorized, error in
guard isAuthorized == true else {
return print(error)
}
print("success")
}
}
这是包含所有错误代码的 TouchID 和 FaceID 的工作代码 (Swift 4)
在任何使用生物识别技术的项目中,将 NSFaceIDUsageDescription 密钥包含在您应用的 Info.plist
文件中。如果没有此密钥,系统将不允许您的应用使用面容 ID。
let authContext = LAContext()
authContext.localizedFallbackTitle = "Use Passcode"
authContext.localizedCancelTitle = "Cancel"
var authError: NSError?
if authContext.canEvaluatePolicy(.deviceOwnerAuthentication, error: &authError) {
evaluatePolicy(policy, context: authContext)
} else {
guard let error = authError else { return }
print("Error: \(error.code)")
checkError(error)
}
private func evaluatePolicy(_ policy: LAPolicy, context: LAContext) {
context.evaluatePolicy(policy, localizedReason: reason) { (success, error) in
if success {
print("Success")
} else {
guard let error = error else { return }
self.checkError(error as NSError)
}
}
}
private func checkError(_ error: NSError) {
guard let error = error as? LAError else { return }
switch error.code {
case .authenticationFailed:
print("authenticationFailed")
requestAuth(policy: .deviceOwnerAuthentication)
case .userFallback:
print("userFallback")
requestAuth(policy: .deviceOwnerAuthentication)
case .userCancel:
print("userCancel")
case .systemCancel:
print("systemCancel")
case .passcodeNotSet:
print("passcodeNotSet")
case .appCancel:
print("appCancel")
case .invalidContext:
print("invalidContext")
case .notInteractive:
print("notInteractive")
default:
checkBioMetricError(error)
}
}
private func checkBioMetricError(_ error: LAError) {
if #available(iOS 11.0, *) {
switch error.code {
case .biometryNotAvailable,
.biometryNotEnrolled,
.biometryLockout:
requestAuth(policy: .deviceOwnerAuthentication)
default: break
}
} else {
switch error.code {
case .touchIDNotAvailable,
.touchIDNotEnrolled,
.touchIDLockout:
requestAuth(policy: .deviceOwnerAuthentication)
default: break
}
}
}