TouchID、FaceID認證的基本使用

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
}

·

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容