简体   繁体   中英

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

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 first runs application:didFinishLaunchingWithOptions whether it is:

  1. A new install from the app store.
  2. Newly updated from a previous version

There is no code in the app currently in the app store to handle this.
The application uses a SQLite database, but for reasons I won't go into here I don't want to use a check for its existence as a solution to this problem.

As a side question, without storing the data manually, is there an SDK I can use to query when an app was installed onto a device? (Preferably iOS 3.0 compatible)

I have seen a similar question , but none of the answers apply to working with existing app store code.

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

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.

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.

GBVersionTracking 是一个很好的pod来跟踪所有版本的历史记录。

[GBVersionTracking isFirstLaunchEver];  

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM