Created
November 21, 2018 13:24
-
-
Save ioskevinshah/d04409cb189e94f964ad1df343211690 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// LocalAuthentication | |
class LocalAuthentication: NSObject { | |
/// Variable Declaration(s) | |
class var isFaceIdAvailable: Bool { | |
let context: LAContext = LAContext() | |
let evaluate = context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: nil) | |
if #available(iOS 11.0, *) { | |
return (context.biometryType == .faceID) | |
} | |
return evaluate | |
} | |
class var isTouchIdAvailable: Bool { | |
let context: LAContext = LAContext() | |
let evaluate = context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: nil) | |
if #available(iOS 11.0, *) { | |
return (context.biometryType == .touchID) | |
} | |
return evaluate | |
} | |
/// Change default message as per your need. | |
fileprivate static var defaultReasonMessage: String { | |
return LocalAuthentication.isFaceIdAvailable ? kFaceIdAuthenticationReason : kTouchIdAuthenticationReason | |
} | |
/// Block Declaration(s) | |
typealias authenticationBlock = ((AuthenticationBlockState) -> ()) | |
} | |
// MARK: - Authentication Related Method(s) | |
extension LocalAuthentication { | |
/// This method will check the user authentication with biometeric. If biometrics get failed user will prompt with passcode option. | |
/// | |
/// - Parameters: | |
/// - reasonMessage: Reason message is must be and cannot be empty/nil for using biometeric/passcode. | |
/// - cancelTitle: By default is `Cancel`. | |
/// - block: Return the state of user authentication is success or failure. | |
class func authenticateUserWithBioMetrics(_ reasonMessage: String, cancelTitle: String? = "", block: @escaping authenticationBlock) { | |
let context: LAContext = LAContext() | |
context.localizedCancelTitle = cancelTitle | |
context.localizedFallbackTitle = "Passcode" | |
evaluate(.deviceOwnerAuthentication, reason: (reasonMessage.isEmpty ? defaultReasonMessage : reasonMessage), context: context, block: block) | |
} | |
fileprivate static func evaluate(_ policy: LAPolicy, reason: String, context: LAContext, block: @escaping authenticationBlock) { | |
context.evaluatePolicy(policy, localizedReason: reason) { (isSuccess, error) in | |
block(AuthenticationBlockState.initWith(error as? LAError)) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment