Skip to content

Instantly share code, notes, and snippets.

View ioskevinshah's full-sized avatar

Kevin Shah ioskevinshah

View GitHub Profile
/// 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
}
/// Change default message as per your need.
fileprivate static var defaultReasonMessage: String {
return LocalAuthentication.isFaceIdAvailable ? kFaceIdAuthenticationReason : kTouchIdAuthenticationReason
}
let kReasonMessage = "Biometeric required your authentication."
/// Biometric Default Reason(s)
let kBiometryNotAvailableReason = "Biometric authentication is not available for this device."
/// Common Message(s)
let kSetPasscode = "Please set device passcode for authentication."
/// TouchID Related Message(s)
let kTouchIdAuthenticationReason = "Confirm your fingerprint to authenticate."
import LocalAuthentication
/// AuthenticationBlockState
enum AuthenticationBlockState {
case success
case failed
case canceledByUser
case fallback
case canceledBySystem
case passcodeNotSet
/// Block Declaration(s)
typealias authenticationBlock = ((AuthenticationBlockState) -> ())
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))
}
}
@IBAction func tapBtnAuthenticate(_ sender: UIButton) {
LocalAuthentication.authenticateUserWithBioMetrics(kReasonMessage) { [weak self] (authenticationState) in
guard let self = self else {
return
}
switch authenticationState {
case .success:
self.showAlert("Successfully Authenticated")
case .canceledByUser, .fallback, .canceledBySystem:
break
/// 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)
}
/// Variable Declaration(s)
var lblTitle: UILabel = {
var lbl: UILabel = UILabel()
lbl.translatesAutoresizingMaskIntoConstraints = false
lbl.backgroundColor = .clear
lbl.textAlignment = .center
lbl.adjustsFontSizeToFitWidth = true
lbl.minimumScaleFactor = 0.75
lbl.textColor = .darkText
lbl.font = .boldSystemFont(ofSize: UIFont.systemFontSize)