(iOS開發)IOS 9 APP版本檢驗更新和跳轉
阿新 • • 發佈:2019-01-29
/** * 判斷app安裝版本和商店版本的比較 */ -(void)judgeAPPVersion { // https://itunes.apple.com/lookup?id=604685049 NSString *urlStr = @"https://itunes.apple.com/lookup?id=604685049"; NSURL *url = [NSURL URLWithString:urlStr]; NSURLRequest *req = [NSURLRequest requestWithURL:url]; [NSURLConnection connectionWithRequest:req delegate:self]; } /** * 解析網路請求下的資料,並提示alertView */ -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { NSError *error; id jsonObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error]; NSDictionary *appInfo = (NSDictionary *)jsonObject; NSArray *infoContent = [appInfo objectForKey:@"results"]; NSString *version = [[infoContent objectAtIndex:0] objectForKey:@"version"]; NSLog(@"商店的版本是 %@",version); NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary]; NSString *currentVersion = [infoDic objectForKey:@"CFBundleShortVersionString"]; NSLog(@"當前的版本是 %@",currentVersion); if (![version isEqualToString:currentVersion]) { UIAlertController * alert=[UIAlertController alertControllerWithTitle:@"提示" message:@"商店有版本更新" preferredStyle:UIAlertControllerStyleAlert]; [alert addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { //確定,跳轉到appstore [self changeToAppStore]; }]]; [alert addAction:[UIAlertAction actionWithTitle:@"NO" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { //取消,不做任何事情 }]]; [self presentViewController:alert animated:YES completion:^{ }]; } } //跳轉到AppStore -(void)changeToAppStore { NSURL *myUrl = [NSURL URLWithString:@"QQMusic://abcde"]; if([[UIApplication sharedApplication] canOpenURL:myUrl]) { [[UIApplication sharedApplication] openURL:myUrl]; }else { [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://itunes.apple.com/cn/app/qq-yin-le-du-jia-zhong-guo/id414603431?mt=8"]]; } } /** * 注意: 在IOS9之後,跳轉白名單需要在info.plist中新增 LSApplicationQueriesSchemes (是個Array型別) 再在Item0 中新增路徑 : QQMusic */ //非同步執行緒去下載 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ [self judgeAPPVersion]; }); //必要時需要返回主程式呼叫方法 dispatch_sync(dispatch_get_main_queue(), ^(){ // 這裡的程式碼會在主執行緒執行 [self presentViewController:alert animated:YES completion:nil]; });