Skip to content

Instantly share code, notes, and snippets.

@thedillonb
Created April 1, 2016 01:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thedillonb/f901945400bceab92c0b24b949cf04e9 to your computer and use it in GitHub Desktop.
Save thedillonb/f901945400bceab92c0b24b949cf04e9 to your computer and use it in GitHub Desktop.
Stamp iOS With Date
public static class UIApplicationDelegateExtensions
{
/// <summary>
/// Record the date this application was installed (or the date that we started recording installation date).
/// </summary>
public static DateTime StampInstallDate(this UIApplicationDelegate @this, string name)
{
try
{
var query = new SecRecord(SecKind.GenericPassword) { Service = name, Account = "account" };
SecStatusCode secStatusCode;
var queriedRecord = SecKeyChain.QueryAsRecord(query, out secStatusCode);
if (secStatusCode != SecStatusCode.Success)
{
queriedRecord = new SecRecord(SecKind.GenericPassword)
{
Label = name + " Install Date",
Service = name,
Account = query.Account,
Description = string.Format("The first date {0} was installed", name),
Generic = NSData.FromString(DateTime.UtcNow.ToString())
};
var err = SecKeyChain.Add(queriedRecord);
if (err != SecStatusCode.Success)
System.Diagnostics.Debug.WriteLine("Unable to save stamp date!");
}
else
{
DateTime time;
if (!DateTime.TryParse(queriedRecord.Generic.ToString(), out time))
SecKeyChain.Remove(query);
}
return DateTime.Parse(NSString.FromData(queriedRecord.Generic, NSStringEncoding.UTF8));
}
catch (Exception e)
{
System.Diagnostics.Debug.WriteLine(e.Message);
return DateTime.Now;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment