Skip to content

Instantly share code, notes, and snippets.

@mshenoy83
Last active September 25, 2017 02:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mshenoy83/7cf173ac348b1509759bc17b363df710 to your computer and use it in GitHub Desktop.
Save mshenoy83/7cf173ac348b1509759bc17b363df710 to your computer and use it in GitHub Desktop.
public class IOSDeviceInfo : IDeviceInfo
{
private string _deviceId;
private const string DeviceIdentifier = "MyDeviceIdentifier";
public string GetDeviceId
{
get
{
/* return UIDevice.CurrentDevice.IdentifierForVendor.ToString();*/
_deviceId = GetRecordsFromKeychain(DeviceIdentifier);
if (!string.IsNullOrEmpty(_deviceId)) return _deviceId;
_deviceId = Guid.NewGuid().ToString();
StoreKeysInKeychain(DeviceIdentifier, _deviceId);
return _deviceId;
}
}
#region Manage KeyChain
private void StoreKeysInKeychain(string key, string value)
{
DeleteKeyFromKeychain(key);
var s = new SecRecord(SecKind.GenericPassword)
{
AccessGroup = "kSecAttrAccessGroupToken",
ValueData = NSData.FromString(value),
Generic = NSData.FromString(key)
};
var result = SecKeyChain.Add(s);
}
private void DeleteKeyFromKeychain(string key)
{
SecStatusCode res;
var rec = new SecRecord(SecKind.GenericPassword)
{
AccessGroup = "kSecAttrAccessGroupToken",
Generic = NSData.FromString(key)
};
var match = SecKeyChain.QueryAsRecord(rec, out res);
if (match != null)
SecKeyChain.Remove(match);
}
private string GetRecordsFromKeychain(string key)
{
try
{
SecStatusCode res;
var rec = new SecRecord(SecKind.GenericPassword)
{
AccessGroup = "kSecAttrAccessGroupToken",
Generic = NSData.FromString(key)
};
var match = SecKeyChain.QueryAsRecord(rec, out res);
if (match != null)
return match.ValueData.ToString();
}
catch (Exception e)
{
Console.WriteLine(e);
}
return string.Empty;
}
#endregion
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment