Skip to content

Instantly share code, notes, and snippets.

@ioskevinshah
Created November 21, 2018 12:55
Show Gist options
  • Save ioskevinshah/be6b4f31cb46b1dba03d73ff99cea6cf to your computer and use it in GitHub Desktop.
Save ioskevinshah/be6b4f31cb46b1dba03d73ff99cea6cf to your computer and use it in GitHub Desktop.
import LocalAuthentication
/// AuthenticationBlockState
enum AuthenticationBlockState {
case success
case failed
case canceledByUser
case fallback
case canceledBySystem
case passcodeNotSet
case biometryNotAvailable
case biometryNotEnrolled
case biometryLockedOut
case other
static func initWith(_ error: LAError?) -> AuthenticationBlockState {
if error == nil {
return .success
} else {
switch Int32(error!.errorCode) {
case kLAErrorAuthenticationFailed:
return .failed
case kLAErrorUserCancel:
return .canceledByUser
case kLAErrorUserFallback:
return .fallback
case kLAErrorSystemCancel:
return .canceledBySystem
case kLAErrorPasscodeNotSet:
return .passcodeNotSet
case kLAErrorBiometryNotAvailable:
return .biometryNotAvailable
case kLAErrorBiometryNotEnrolled:
return .biometryNotEnrolled
case kLAErrorBiometryLockout:
return .biometryLockedOut
default:
return other
}
}
}
func message() -> String {
switch self {
case .canceledByUser, .fallback, .canceledBySystem:
return ""
case .passcodeNotSet:
return kSetPasscode
case .biometryNotAvailable:
return kBiometryNotAvailableReason
case .biometryNotEnrolled:
return LocalAuthentication.isFaceIdAvailable ? kNoFaceIdentityEnrolled : kNoFingerprintEnrolled
case .biometryLockedOut:
return LocalAuthentication.isFaceIdAvailable ? kFaceIdPasscodeAuthenticationReason : kTouchIdPasscodeAuthenticationReason
default:
return LocalAuthentication.isFaceIdAvailable ? kDefaultFaceIDAuthenticationFailedReason : kDefaultTouchIDAuthenticationFailedReason
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment