先上代码
//1. 在application中调用 -(void)checkVersionUpdata{ NSString *urlStr = @"http://itunes.apple.com/lookup?id=1329918420";//id替换即可 NSURL *url = [NSURL URLWithString:urlStr]; NSURLRequest *req = [NSURLRequest requestWithURL:url]; [NSURLConnection connectionWithRequest:req delegate:self]; } //2. 网络连接 -(void)connection:(NSURLConnection *)connection didReceiveData:(nonnull NSData *)data { NSError *error; id jsonObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error]; [XKCEHint disMissLoading]; NSDictionary *appInfo = (NSDictionary*)jsonObject; NSArray *infoContent = [appInfo objectForKey:@"results"]; NSString * version = [[infoContent objectAtIndex:0]objectForKey:@"version"];//线上最新版本 // 获取当前版本 NSString *currentVersion = [self version];//当前用户版本 BOOL result = [currentVersion compare:version] == NSOrderedAscending; if (result) {//需要更新 [XKCEGlobleData sharedData].version = @"1"; NSLog(@"不是最新版本需要更新"); NSString *updateStr = [NSString stringWithFormat:@"发现新版本V%@/n为保证软件的正常运行/n请及时更新到最新版本",version]; [self creatAlterView:updateStr]; } else {//已经是最新版; NSLog(@"最新版本不需要更新"); } } //3. 弹框提示 -(void)creatAlterView:(NSString *)msg{ UIAlertController *alertText = [UIAlertController alertControllerWithTitle:@"更新提醒" message:msg preferredStyle:UIAlertControllerStyleAlert]; //增加按钮 [alertText addAction:[UIAlertAction actionWithTitle:@"我再想想" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { }]]; [alertText addAction:[UIAlertAction actionWithTitle:@"立即更新" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { NSString *str = @"itms-apps://itunes.apple.com/cn/app/id1329918420?mt=8"; //更换id即可 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]]; }]]; [self.window.rootViewController presentViewController:alertText animated:YES completion:nil]; } //版本 -(NSString *)version { NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary]; NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"]; return app_Version; }
1. url只要替换成自己的id即可
2. jsonobject中可以获取到当前软件在APPstore的信息,返回的内容大致如下
{ resultCount = 1; results = ( { artistId = 开发者 ID; artistName = 开发者名称; price = 0; isGameCenterEnabled = 0; kind = software; languageCodesISO2A = ( EN ); trackCensoredName = 审查名称; trackContentRating = 评级; trackId = 应用程序 ID; trackName = 应用程序名称"; trackViewUrl = 应用程序介绍网址; userRatingCount = 用户评级; userRatingCountForCurrentVersion = 1; version = 版本号; wrapperType = software; } ); }
我们需要的就是版本信息,将当前版本与AppStore获取的版本相对比,查看是否是最新版本
3. 弹框提示
跳转到APPstore的url可以直接将id替换。