Skip to content

Instantly share code, notes, and snippets.

View ioskevinshah's full-sized avatar

Kevin Shah ioskevinshah

View GitHub Profile
override init(frame: CGRect) {
super.init(frame: frame)
/// Preparing UIView
prepareUIView()
}
fileprivate var verticalStackView: UIStackView = {
let stackView: UIStackView = UIStackView()
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.isUserInteractionEnabled = false
stackView.alignment = .center
stackView.axis = .vertical
stackView.distribution = .equalCentering
return stackView
}()
var lblSubTitle: UILabel = {
var lbl: UILabel = UILabel()
lbl.translatesAutoresizingMaskIntoConstraints = false
lbl.backgroundColor = .clear
lbl.textAlignment = .center
lbl.adjustsFontSizeToFitWidth = true
lbl.minimumScaleFactor = 0.75
lbl.textColor = .darkGray
lbl.font = .systemFont(ofSize: UIFont.smallSystemFontSize)
return lbl
/// 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)
/// 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)
}
@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
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))
}
}
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)
}
/// Block Declaration(s)
typealias authenticationBlock = ((AuthenticationBlockState) -> ())