Skip to content

Instantly share code, notes, and snippets.

@evansobkowicz
Created April 4, 2015 19:56
Show Gist options
  • Save evansobkowicz/f325d28950499fca46c8 to your computer and use it in GitHub Desktop.
Save evansobkowicz/f325d28950499fca46c8 to your computer and use it in GitHub Desktop.
RubyMotion Touch ID
# Call touch_id_authenticate
def touch_id_authenticate
context = LAContext.alloc.init
auth_error = nil
reason = 'Authenticate using your fingerprint'
if context.canEvaluatePolicy(LAPolicyDeviceOwnerAuthenticationWithBiometrics, error: auth_error)
context.evaluatePolicy(LAPolicyDeviceOwnerAuthenticationWithBiometrics, localizedReason: reason, reply: lambda do |success, error|
if success
Dispatch::Queue.main.async do
successful_authentication
end
else
# Customize Touch ID Failure
case error.code
when LAErrorAuthenticationFailed
auth_alert('Error', 'Authentication Failed')
break
when LAErrorUserCancel
auth_alert('Error', 'User pressed Cancel button')
break
when LAErrorUserFallback
auth_alert('Error', 'User pressed \'Enter Password\'')
break
else
auth_alert('Error', 'Touch ID is not configured')
break
end
end
end)
else
auth_alert('Error', 'No Touch ID on Device')
end
end
def auth_alert(title, message)
Dispatch::Queue.main.async do
@alert = UIAlertView.alloc.initWithTitle(
title,
message: message,
delegate: nil,
cancelButtonTitle: 'Dismiss',
otherButtonTitles: nil,
)
@alert.show
end
end
def successful_authentication
# Open a new screen, etc.
end
@dam13n
Copy link

dam13n commented Aug 1, 2016

for people that find this, try using KLAPolicyDeviceOwnerAuthenticationWithBiometrics instead of LAPolicyDeviceOwnerAuthenticationWithBiometrics if you have problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment