Created
June 3, 2016 13:29
-
-
Save KalpeshTalkar/dc3c22480d29926153b029265e2afc0d 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
// | |
// TouchIDAuthenticationObject.swift | |
// | |
// Created by Kalpesh Talkar on 03/06/16. | |
// Copyright © 2016 Kalpesh. All rights reserved. | |
// | |
import UIKit | |
import LocalAuthentication | |
class TouchIDAuthenticationObject: NSObject { | |
let context = LAContext() | |
var error: NSError? | |
func authenticateWithReason(reason: String, completion:(success: Bool, error: NSError?, laerror: LAError?) -> Void) { | |
if context.canEvaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, error:&error) { | |
context.evaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, localizedReason: reason, reply: { (success, error) in | |
if success { | |
completion(success: success, error: error, laerror: nil) | |
} else { | |
switch error!.code { | |
case LAError.AuthenticationFailed.rawValue: | |
// authentication failed | |
completion(success: success, error: error, laerror: LAError.AuthenticationFailed) | |
case LAError.UserCancel.rawValue: | |
// user canceled authentication | |
completion(success: success, error: error, laerror: LAError.UserCancel) | |
case LAError.SystemCancel.rawValue: | |
// system canceled authentication | |
completion(success: success, error: error, laerror: LAError.SystemCancel) | |
case LAError.PasscodeNotSet.rawValue: | |
// passcode not set | |
completion(success: success, error: error, laerror: LAError.PasscodeNotSet) | |
case LAError.UserFallback.rawValue: | |
// user chose password | |
completion(success: success, error: error, laerror: LAError.UserFallback) | |
default: | |
// unable to authenticate user | |
completion(success: success, error: error, laerror: nil) | |
} | |
} | |
}) | |
} else { | |
switch error!.code { | |
case LAError.TouchIDNotAvailable.rawValue: | |
// Touch ID not available on device | |
completion(success: false, error: error, laerror: LAError.TouchIDNotAvailable) | |
case LAError.TouchIDNotEnrolled.rawValue: | |
// Touch ID is not enrolled yet" | |
completion(success: false, error: error, laerror: LAError.TouchIDNotEnrolled) | |
case LAError.PasscodeNotSet.rawValue: | |
// Passcode not set | |
completion(success: false, error: error, laerror: LAError.PasscodeNotSet) | |
default: | |
// Local authentication not available | |
completion(success: false, error: error, laerror: nil) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment