Skip to content

Instantly share code, notes, and snippets.

@spnkr
Forked from evansobkowicz/touch_id.rb
Created October 8, 2015 07:59
Show Gist options
  • Save spnkr/5af4c274d511ffac01c5 to your computer and use it in GitHub Desktop.
Save spnkr/5af4c274d511ffac01c5 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment