如何判断一个iOS应用程序是否已经新安装或更新?

我目前在app store有一个应用程序,我打算很快提交更新。

有了这个更新,我想添加代码,它会告诉应用程序,当它第一次运行application:didFinishLaunchingWithOptions无论是:

  1. 从应用程序商店的新安装。
  2. 从以前的版本新的更新

目前应用程序商店中的应用程序没有代码来处理这个问题。
该应用程序使用SQLite数据库,但出于某些原因,我不会进入这里,我不想使用检查它的存在作为解决这个问题。

作为一个侧面的问题,没有手动存储数据,是否有一个SDK可以用来查询应用程序何时安装到设备上? (最好与iOS 3.0兼容)

我看到了类似的问题 ,但是没有一个答案适用于使用现有的app store代码。

您可以将版本号保存到NSUserDefaults ,并相应地进行更新。

如果这样做不起作用,您可以发布介绍版本控制scheme的中间版本。

如果这不是一个选项,您可能能够检查从您创build的文件,或您有条件或延迟设置的偏好之前运行的痕迹。

以下代码可能有助于解答有关应用程序安装时间的问题。 我不确定如果应用程序包创builddate是XCode生成date或下载date,因为这是从应用程序商店未经testing。

 NSString *bundleRoot = [[NSBundle mainBundle] bundlePath]; // eg /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]; // eg /var/mobile/Applications/<GUID> attrs = [manager attributesOfItemAtPath:rootPath error:nil]; NSLog(@"Date/Time first installed (or first reinstalled after deletion): %@", [attrs fileCreationDate]); 

试试这个代码,我知道我为这个答案为时已晚,但为了knwoldge分享我在这里。

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

注意

ID: – 用您自己的应用程序IDreplaceID。 您可以从itune获取应用程序ID连接选定的应用程序 – >更多信息 – >元数据

由于模拟器没有应用程序商店的应用程序,这个代码将无法在模拟器上工作。

GBVersionTracking可以跟踪所有的版本历史。

 [GBVersionTracking isFirstLaunchEver]; 

这里有一个简单的代码来知道当前版本是否不同(这个代码也可以在模拟器上运行)。

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

注意:确保在iTunes中input新版本时,这与您要发布的应用程序中的版本相匹配。 如果没有,那么无论用户是否更新,上面的代码将总是返回YES。