使用钥匙串
工作源 - https://github.com/benhysell/V.TouchIdExample
长表格说明 - http://benjaminhysell.com/archive/2014/11/authentication-in-xamarin-ios-with-touch-id-or-passcode/
//Simple View with a switch to enable / disable Touch ID and
//a button to invoke authentication
/// <summary>
/// Enable/Disable Touch ID
/// </summary>
/// <param name="sender">Sender.</param>
partial void TouchIdEnableDisable(UISwitch sender)
{
if (sender.On)
{
//enable Touch ID
//set our record
//note what you fill in here doesn't matter, just needs to be
//consistent across all uses of the record
var secRecord = new SecRecord(SecKind.GenericPassword)
{
Label = "Keychain Item",
Description = "fake item for keychain access",
Account = "Account",
Service = "com.yourcompany.touchIdExample",
Comment = "Your comment here",
ValueData = NSData.FromString("my-secret-password"),
Generic = NSData.FromString("foo")
};
secRecord.AccessControl = new SecAccessControl(SecAccessible.WhenPasscodeSetThisDeviceOnly, SecAccessControlCreateFlags.UserPresence);
SecKeyChain.Add(secRecord);
authenticateButton.Enabled = true;
}
else
{
//disable Touch ID
var record = new SecRecord(SecKind.GenericPassword)
{
Service = "com.yourcompany.touchIdExample",
UseOperationPrompt = "Authenticate to Remove Touch ID / Passcode from Test App"
};
SecStatusCode result;
//query one last time to ensure they can remove it
SecKeyChain.QueryAsRecord(record, out result);
if (SecStatusCode.Success == result || SecStatusCode.ItemNotFound == result)
{
//remove the record
SecKeyChain.Remove(record);
authenticateButton.Enabled = false;
}
else
{
//could not authenticate, leave switch on
sender.On = true;
}
}
}
/// <summary>
/// Show Touch ID to user and evaluate authentication
/// </summary>
/// <param name="sender">Sender.</param>
partial void AuthenticateUser(UIButton sender)
{
var rec = new SecRecord(SecKind.GenericPassword)
{
Service = "com.yourcompany.touchIdExample",
UseOperationPrompt = "Authenticate to access Test App"
};
SecStatusCode res;
SecKeyChain.QueryAsRecord(rec, out res);
if (SecStatusCode.Success == res || SecStatusCode.ItemNotFound == res)
{
//Success!!
//add your code here to continue into your application
AuthenticatedLabel.Hidden = false;
}
else
{
//Failure
AuthenticatedLabel.Hidden = true;
}
}