I\'m planning on opening up an in app store and I\'d like to give existing users some of the items for free.
I thought of releasing an update which would store some
You can always check on the server. It used to be simple with UDID but since these are deprecated you have create a UUID (CFUUIDCreate() or something like that :)) and store it in the keychain, every information stored in the keychain will stay persistent even if user will delete the app and installs again after a year or so ... I found this articke really helpful when handling the keychain values for the first time: http://useyourloaf.com/blog/2010/3/29/simple-iphone-keychain-access.html
NSDate *installDate = [[NSUserDefaults standardUserDefaults]objectForKey:@"installDate"];
if (!installDate) {
//no date is present
//this app has not run before
NSDate *todaysDate = [NSDate date];
[[NSUserDefaults standardUserDefaults]setObject:todaysDate forKey:@"installDate"];
[[NSUserDefaults standardUserDefaults]synchronize];
//nothing more to do?
} else {
//date is found
NSDate *todaysDate = [NSDate date];
//compare todaysDate with installDate
//if enough time has passed, yada yada,
//apply updates, etc.
}
Swift solution:
let urlToDocumentsFolder = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).last!
//installDate is NSDate of install
let installDate = (try! NSFileManager.defaultManager().attributesOfItemAtPath(urlToDocumentsFolder.path!)[NSFileCreationDate])
print("This app was installed by the user on \(installDate)")
I'd recommend simply setting a date on first launch and storing it in NSUserDefaults. Look for the date on launch, if it's not there, set it. If it is, do some date comparison and handle the rest according to your plan.
To get the installation date, check the creation date of the Documents folder.
NSURL* urlToDocumentsFolder = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
__autoreleasing NSError *error;
NSDate *installDate = [[[NSFileManager defaultManager] attributesOfItemAtPath:urlToDocumentsFolder.path error:&error] objectForKey:NSFileCreationDate];
NSLog(@"This app was installed by the user on %@", installDate);
To get the date of the last App update, check the modification date of the App bundle itself
NSString* pathToInfoPlist = [[NSBundle mainBundle] pathForResource:@"Info" ofType:@"plist"];
NSString* pathToAppBundle = [pathToInfoPlist stringByDeletingLastPathComponent];
NSDate *updateDate = [[[NSFileManager defaultManager] attributesOfItemAtPath:pathToAppBundle error:&error] objectForKey:NSFileModificationDate];
NSLog(@"This app was updated by the user on %@", updateDate);
Answering to see if people can find fault in this. It seemed longer than the others I find here, but it does tell you if app is running for the first time since updating (for this you have to change your info.plist)...
// called during startup, compares the current version string with the one stored on standardUserDefaults
+ (BOOL)isFreshInstallOrUpdate
{
BOOL ret = YES;
NSString *cfBundleVersionStored = [[NSUserDefaults standardUserDefaults] objectForKey:@"CFBundleVersion"];
NSString *cfBundleShortVersionStringStored = [[NSUserDefaults standardUserDefaults] objectForKey:@"CFBundleShortVersionString"];
NSString *cfBundleVersionPlist = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"];;
NSString *cfBundleShortVersionStringPlist = [[NSBundle mainBundle] objectForInfoDictionaryKey:
@"CFBundleShortVersionString"];
ret = [cfBundleVersionPlist compare:cfBundleVersionStored] != NSOrderedSame ||
[cfBundleShortVersionStringPlist compare:cfBundleShortVersionStringStored] != NSOrderedSame;
return ret;
}
// calling this once will necessarily cause isFreshInstall to return false
+ (void)setStoredVersionNumber
{
NSString *cfBundleVersionPlist = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"];
NSString *cfBundleShortVersionStringPlist = [[NSBundle mainBundle] objectForInfoDictionaryKey:
@"CFBundleShortVersionString"];
[[NSUserDefaults standardUserDefaults] setObject:cfBundleVersionPlist forKey:@"CFBundleVersion"];
[[NSUserDefaults standardUserDefaults] setObject:cfBundleShortVersionStringPlist forKey:@"CFBundleShortVersionString"];
// setting now as unfresh!
[[NSUserDefaults standardUserDefaults] synchronize];
}