一、版本的比較網絡
//處理版本 -(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