Created
April 1, 2016 01:01
-
-
Save thedillonb/f901945400bceab92c0b24b949cf04e9 to your computer and use it in GitHub Desktop.
Stamp iOS With Date
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 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