Last active
September 25, 2017 02:31
-
-
Save mshenoy83/7cf173ac348b1509759bc17b363df710 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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