-
Notifications
You must be signed in to change notification settings - Fork 0
/
ViewController.cs
107 lines (84 loc) · 2.83 KB
/
ViewController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
using System;
using UIKit;
using LocalAuthentication;
using Foundation;
namespace DotNetMiami
{
public partial class ViewController : UIViewController
{
public ViewController (IntPtr handle) : base (handle)
{
}
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
// Perform any additional setup after loading the view, typically from a nib.
}
public override void DidReceiveMemoryWarning ()
{
base.DidReceiveMemoryWarning ();
// Release any cached data, images, etc that aren't in use.
}
public override bool ShouldPerformSegue (string segueIdentifier, NSObject sender)
{
if (segueIdentifier == "SegueToTest") {
return false; // Return false, let btnLogin handle Segue
}
return true;
}
partial void btnLogin_TouchUpInside (UIButton sender)
{
NSError _error;
using (var _context = new LAContext ()) {
// Allows us to reuse Touch ID verificaton from unlocking device up to 30 seconds later
_context.TouchIdAuthenticationAllowableReuseDuration = 30;
if (_context.CanEvaluatePolicy (LAPolicy.DeviceOwnerAuthenticationWithBiometrics, out _error)) {
// Get enrollment state
if (_context.EvaluatedPolicyDomainState != null) {
var policyState = _context.EvaluatedPolicyDomainState.ToString().Replace("<", "").Replace(">", "").Replace(" ", "");
if (TouchIDPolicyDomainState != null)
{
if (policyState != TouchIDPolicyDomainState)
Console.WriteLine("Fingerprints enrollments changed.");
else
Console.WriteLine("Fingerprints enrollments remain the same.");
}
// Store enrollment
TouchIDPolicyDomainState = policyState;
}
var replyHandler = new LAContextReplyHandler ((success, error) => {
InvokeOnMainThread (() => {
if (success) {
PerformSegue("SegueToTest", null);
} else {
DisplayAlertOKPopup("", "Unable to authenticate.");
}
});
});
// Add reason why we are using Touch ID
_context.EvaluatePolicy (LAPolicy.DeviceOwnerAuthenticationWithBiometrics, "Login as user: X", replyHandler);
} else {
DisplayAlertOKPopup("", "Touch ID not available on this device.");
}
}
}
public void DisplayAlertOKPopup(string title, string message)
{
var act = UIAlertAction.Create ("OK", UIAlertActionStyle.Default, null);
using (var alertCntrl = UIAlertController.Create (title, message, UIAlertControllerStyle.Alert)) {
alertCntrl.AddAction (act);
this.PresentViewController(alertCntrl, true, null);
}
}
public static string TouchIDPolicyDomainState
{
get {
return NSUserDefaults.StandardUserDefaults.StringForKey("TouchIDPolicyDomainState");
}
set {
NSUserDefaults.StandardUserDefaults.SetString(value, "TouchIDPolicyDomainState");
NSUserDefaults.StandardUserDefaults.Synchronize ();
}
}
}
}