Skip to content

Instantly share code, notes, and snippets.

@hansemannn
Created March 22, 2016 11:39
Show Gist options
  • Select an option

  • Save hansemannn/c3f65193cbe8f904a6ab to your computer and use it in GitHub Desktop.

Select an option

Save hansemannn/c3f65193cbe8f904a6ab to your computer and use it in GitHub Desktop.
import Foundation
import UIKit
import LocalAuthentication
public class TiTouchId {
var context: LAContext?
init() {
context = LAContext()
}
func isSupported() -> Bool {
var error: NSError?
let currentOSSupported: Bool = UIDevice.currentDevice().systemVersion.compare("8.0", options: .NumericSearch) != .OrderedAscending
let touchIDSupported: Bool = context!.canEvaluatePolicy(.DeviceOwnerAuthenticationWithBiometrics, error: &error)
return currentOSSupported && touchIDSupported
}
func authenticate(reason: String, callback: (NSMutableDictionary) -> ()) -> Void {
context?.evaluatePolicy(.DeviceOwnerAuthenticationWithBiometrics, localizedReason: reason, reply: {(success, error) in
callback([
"success": success,
"error": (error?.localizedDescription)!,
"code": (error?.code)!
])
})
}
}
let touchId = TiTouchId()
if (touchId.isSupported()) {
touchId.authenticate("Please verify to continue", callback: {(attrs) in
let success: Bool = attrs.valueForKey("success") as! Bool
NSLog("Success - %d", success)
if ((attrs.valueForKey("error")) != nil) {
NSLog("Error - %@", attrs.valueForKey("error") as! String)
NSLog("Code - %lu", attrs.valueForKey("code") as! Int)
}
})
} else {
NSLog("Touch ID is not supported on this device")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment