一、版本的比较网络
//处理版本 -(void)VersonUpdate{ //定义的app的地址 NSString *urld = [NSString stringWithFormat:@"http://itunes.apple.com/lookup?id=%@",@"你的id"]; //网络请求app的信息,主要是取得我说须要的Version NSURL *url = [NSURL URLWithString:urld]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10]; [request setHTTPMethod:@"POST"]; NSURLSession *session = [NSURLSession sharedSession]; NSURLSessionDataTask *task = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { NSMutableDictionary *receiveStatusDic=[[NSMutableDictionary alloc]init]; if (data) { //data是有关于App全部的信息 NSDictionary *receiveDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil]; if ([[receiveDic valueForKey:@"resultCount"] intValue]>0) { [receiveStatusDic setValue:@"1" forKey:@"status"]; [receiveStatusDic setValue:[[[receiveDic valueForKey:@"results"] objectAtIndex:0] valueForKey:@"version"] forKey:@"version"]; //请求的有数据,进行版本比较 [self performSelectorOnMainThread:@selector(receiveData:) withObject:receiveStatusDic waitUntilDone:NO]; }else{ [receiveStatusDic setValue:@"-1" forKey:@"status"]; } }else{ [receiveStatusDic setValue:@"-1" forKey:@"status"]; } }]; [task resume]; }
二、获取自身版本号session
-(void)receiveData:(id)sender { //获取APP自身版本号 localVersion 例如:0.2.1 -> 021 NSString *localVersion = [[[NSBundle mainBundle]infoDictionary]objectForKey:@"CFBundleShortVersionString"]; localVersion = [localVersion stringByReplacingOccurrencesOfString:@"." withString:@""]; NSString * serverVerison = sender[@"version"]; serverVerison = [serverVerison stringByReplacingOccurrencesOfString:@"." withString:@""]; if ([serverVerison intValue]>[localVersion intValue]) { [self updateVersion]; } }
三、提示更新app
-(void)updateVersion{ NSString *msg = [NSString stringWithFormat:@"更新最新版本,优惠信息提早知"]; UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"升级提示" message:msg preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *otherAction = [UIAlertAction actionWithTitle:@"如今升级"style:UIAlertActionStyleDestructive handler:^(UIAlertAction*action) { NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://itunes.apple.com/cn/app/id1355602256?mt=8"]]; [[UIApplication sharedApplication]openURL:url]; }]; [alertController addAction:otherAction]; [self.window.rootViewController presentViewController:alertController animated:YES completion:nil];//@"1355602256" }
此方法是要上架后才能够用!url
欢迎关注个人公众号:
code