How to tell if an iOS application has been newly installed or updated?

后端 未结 5 1358
隐瞒了意图╮
隐瞒了意图╮ 2020-12-18 01:05

I have an application currently on the app store which I intend to submit an update for soon.

With this update I want to add code which will tell the app when it fi

相关标签:
5条回答
  • 2020-12-18 01:27

    try this code, i know i am too late for this answer but for knwoldge sharing here i go.

        -(void) checkIsAppUpdated
    {
    NSString *urlString = @"http://itunes.apple.com/lookup?id=995558215";
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]];
    
    [request setHTTPMethod:@"GET"];
    
    NSError *error = nil;
    NSURLResponse *response = nil;
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    
    //NSString *stringReply = (NSString *)[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    
    if (error)
    {
       // NSLog(@"Error: %@", stringReply);
        //error reviced
    
    }
    else
    {
        //The response is in data
        //NSLog(@"Success: %@", stringReply);
        NSDictionary *dictResponse = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        float appStoreVersion=[[[[dictResponse objectForKey:@"results"] firstObject] objectForKey:@"version"] floatValue];
    
        NSLog(@"app stroe version=%f",appStoreVersion);
    
        NSString *strLocalVersion=[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
        float localAppVersion=[strLocalVersion floatValue];
        if (localAppVersion!=appStoreVersion)
        {
            //open app store url
           // NSString *iTunesLink = @"itms://itunes.apple.com/us/app/apple-store/id375380948?mt=8";
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://itunes.apple.com/app/washthenfold/id995558215?mt=8"]];
        }
    }
    
     }
    

    note

    id:-replace id with your own app id. you can get app id from itune connect selected app->more info->meta data

    since simulator doesn't have app store app, this code won't work on simulator.

    0 讨论(0)
  • 2020-12-18 01:29

    Here is a simple code to know if the current version is different (this code work on simulator too.)

    -(BOOL) needsUpdate
    {
        NSDictionary* infoDictionary = [[NSBundle mainBundle] infoDictionary];
        NSString* appID = infoDictionary[@"CFBundleIdentifier"];
        NSURL* url = [NSURL URLWithString:[NSString stringWithFormat:@"http://itunes.apple.com/lookup?bundleId=%@", appID]];
        NSData* data = [NSData dataWithContentsOfURL:url];
        NSDictionary* lookup = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
    
        if ([lookup[@"resultCount"] integerValue] == 1)
        {
            NSString* appStoreVersion = lookup[@"results"][0][@"version"];
            NSString* currentVersion = infoDictionary[@"CFBundleShortVersionString"];
            if (![appStoreVersion isEqualToString:currentVersion])
            {
                NSLog(@"Need to update [%@ != %@]", appStoreVersion, currentVersion);
                return YES;
            }
        }
        return NO;
    }
    

    Note: Make sure that when you enter the new version in iTunes, this matches the version in the app you are releasing. If not then the above code will always return YES regardless if the user updates.

    0 讨论(0)
  • 2020-12-18 01:31

    The following code may help to answer your side question about when an app was installed. I am unsure if the app bundle create date is the XCode build date or the download date as this is untested from app store.

    NSString *bundleRoot = [[NSBundle mainBundle] bundlePath]; // e.g. /var/mobile/Applications/<GUID>/<AppName>.app
    NSFileManager *manager = [NSFileManager defaultManager];
    NSDictionary* attrs = [manager attributesOfItemAtPath:bundleRoot error:nil];
    NSLog(@"Build or download Date/Time of first  version to be installed: %@", [attrs fileCreationDate]);
    NSLog(@"Date/Time of last install (unless bundle changed by code): %@", [attrs fileModificationDate]);
    NSString *rootPath = [bundleRoot substringToIndex:[bundleRoot rangeOfString:@"/" options:NSBackwardsSearch].location]; // e.g /var/mobile/Applications/<GUID>
    attrs = [manager attributesOfItemAtPath:rootPath error:nil];
    NSLog(@"Date/Time first installed (or first reinstalled after deletion): %@", [attrs fileCreationDate]);
    
    0 讨论(0)
  • 2020-12-18 01:37

    You could save a version number to NSUserDefaults, and update it accordingly.

    If that won't work, you may be able to release an intermediate version which introduces the versioning scheme.

    If that's not an option, you may be able to check for traces of previous runs from files you create, or preferences which you set conditionally or lazily.

    0 讨论(0)
  • 2020-12-18 01:39

    GBVersionTracking is good pod to track all version history.

    [GBVersionTracking isFirstLaunchEver];  
    
    0 讨论(0)
提交回复
热议问题