IOS APP检测更新
APPStore上架应用:
第三方库:QJCheckVersionUpdate
核心代码:
+ (void)CheckVerion:(UpdateBlock)updateblock { NSString *storeString = [NSString stringWithFormat:@"https://itunes.apple.com/lookup?bundleId=%@",OLDVERSION]; NSURL *storeURL = [NSURL URLWithString:storeString]; NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:storeURL]; [request setHTTPMethod:@"GET"]; NSOperationQueue *queue = [[NSOperationQueue alloc] init]; [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { if ( [data length] > 0 && !error ) { // Success NSDictionary *appData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil]; dispatch_async(dispatch_get_main_queue(), ^{ // All versions that have been uploaded to the AppStore NSArray *versionsInAppStore = [[appData valueForKey:@"results"] valueForKey:@"version"]; /** * 以上网络请求可以改成自己封装的类 */ if(![versionsInAppStore count]) { NSLog(@"No versions of app in AppStore"); return; } else { NSString *currentAppStoreVersion = [versionsInAppStore objectAtIndex:0]; NSLog(@"%@",OLDVERSION); if ([QJCheckVersionUpdate versionlessthan:[GetUserDefaut isKindOfClass:[NSString class]] && GetUserDefaut ? GetUserDefaut : OLDVERSION Newer:currentAppStoreVersion]) { NSLog(@"暂不更新"); }else{ NSLog(@"请到appstore更新%@版本",currentAppStoreVersion); /** * 修复问题描述 */ NSString *describeStr = [[[appData valueForKey:@"results"] valueForKey:@"releaseNotes"] objectAtIndex:0]; NSLog(@"修复问题描述:%@",describeStr); NSArray *dataArr = [QJCheckVersionUpdate separateToRow:describeStr]; if (updateblock) { updateblock(currentAppStoreVersion,dataArr); } } } }); } }]; }
版本比较
+ (BOOL)versionlessthan:(NSString *)oldOne Newer:(NSString *)newver { if ([oldOne isEqualToString:newver]) { return YES; }else{ if ([oldOne compare:newver options:NSNumericSearch] == NSOrderedDescending) { return YES; }else{ return NO; } } return NO; }
企业INhouse打包的App:
1.方式一:
后台写段代码,记录版本号,每次更新手动修改版本号。
2.方式二:
解析plist。因为打包的时候plist里存着版本号
代码如下:
-(void)updateApp{ NSDictionary* dict = [NSDictionary dictionaryWithContentsOfURL:[NSURL URLWithString:@"http://你服务器上plist的地址/Picc.plist"]]; if (dict) { NSArray* list = [dict objectForKey:@"items"]; NSDictionary* dict2 = [list objectAtIndex:0]; NSDictionary* dict3 = [dict2 objectForKey:@"metadata"]; NSString* newVersion = [dict3 objectForKey:@"bundle-version"]; // NSLog(@"版本号%@",dict4); NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary]; NSString *myVersion = [infoDict objectForKey:@"CFBundleShortVersionString"]; if (![newVersion isEqualToString:myVersion]) { UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"有新版本" delegate:self cancelButtonTitle:@"马上去更新" otherButtonTitles:@"暂不更新", nil]; [alert showWithCompletionHandler:^(NSInteger buttonIndex) { if (buttonIndex ==0) { NSLog(@"更新"); NSLog(@"%@",DOWNLOAD); [[UIApplication sharedApplication] openURL:[NSURL URLWithString:DOWNLOAD]]; } else if(buttonIndex ==1){ NSLog(@"不更新"); } }]; } else{ UIAlertView * aler = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您已经是最新版" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil]; [aler show]; } } else{ UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请检查网络" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil]; [alert show]; } }