1、Info.plist新增描述(此項針對FaceID)
<key>NSFaceIDUsageDescription</key>
<string>需要使用您的面容識別進行安全認證</string>
<key>UIApplicationSceneManifest</key>
2、導入庫
import LocalAuthentication
3、寫一個函數,實現如下代碼
現在應該不會還有支持8.0一下的App了吧?如果有請自行添加判斷
let context = LAContext()
var error: NSError?
if (context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics,
error: &error)) {
context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics,
localizedReason: "驗證") { access, resError in
if (access) {
print("驗證成功了")
} else {
guard let error = resError as? LAError else { return }
print("error is", error)
}
}
}
4、iOS 11
以后
可以通過LAContext().biometryType
進行進一步的判斷,但需要注意,需要在canEvaluatePolicy
之后
@available(iOS 11.0, *)
public enum LABiometryType : Int {
/// The device does not support biometry.
@available(iOS 11.2, *)
case none = 0
/// The device does not support biometry.
@available(iOS, introduced: 11.0, deprecated: 11.2, renamed: "LABiometryType.none")
public static var LABiometryNone: LABiometryType { get }
/// The device supports Touch ID.
case touchID = 1
/// The device supports Face ID.
case faceID = 2
}
·