How to determine the date an app is installed or used for the first time?

后端 未结 10 615
慢半拍i
慢半拍i 2020-12-03 01:40

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

10条回答
  •  感情败类
    2020-12-03 02:16

    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];
    }
    

提交回复
热议问题