今天研究了一下app升級(jí)提示的流程,不難很簡(jiǎn)單。希望小白們能用得上。。。
<UIAlertViewDelegate>
/**
* 判斷app安裝版本和商店版本的比較
*
* PS:還是介紹一下對(duì)應(yīng)信息
* trackCensoredName = 審查名稱(chēng);
* trackContentRating = 評(píng)級(jí);
* trackId = 應(yīng)用程序 ID;
* trackName = 應(yīng)用程序名稱(chēng);
* trackViewUrl = 應(yīng)用程序介紹網(wǎng)址;
* userRatingCount = 用戶(hù)評(píng)級(jí);
* userRatingCountForCurrentVersion = 1;
* version = 版本號(hào);
*/
-(void)judgeAPPVersion
{
// 快捷方式獲得session對(duì)象
NSURLSession *session = [NSURLSession sharedSession];
NSURL *url = [NSURL URLWithString:@"https://itunes.apple.com/lookup?id=1048992038"];
// 通過(guò)URL初始化task,在block內(nèi)部可以直接對(duì)返回的數(shù)據(jù)進(jìn)行處理
NSURLSessionTask *task = [session dataTaskWithURL:url
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSLog(@"%@", [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]);
NSError *errorJs;
id jsonObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&errorJs];
NSDictionary *appInfo = (NSDictionary *)jsonObject;
NSArray *infoContent = [appInfo objectForKey:@"results"];
NSString *version = [[infoContent objectAtIndex:0] objectForKey:@"version"];
NSLog(@"商店的版本是 %@",version);
NSString *skipUrl = [[infoContent objectAtIndex:0] objectForKey:@"trackViewUrl"];
NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
NSString *currentVersion = [infoDic objectForKey:@"CFBundleShortVersionString"];
NSLog(@"當(dāng)前的版本是 %@",currentVersion);
if (![version isEqualToString:currentVersion]) {
// 跳轉(zhuǎn)appstore
[self skipAppStore:skipUrl];
}
}];
// 啟動(dòng)任務(wù)
[task resume];
}
- (void)skipAppStore:(NSString*)strSkipUrl{
// 初始化
UIAlertController *alertDialog = [UIAlertController alertControllerWithTitle:@"是否更新新版本" message:nil preferredStyle:UIAlertControllerStyleAlert];
// 分別創(chuàng)建操作
UIAlertAction *laterAction = [UIAlertAction actionWithTitle:@"更新" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
// App 跳轉(zhuǎn)
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:strSkipUrl]];
}];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
// 取消按鍵
}];
// 添加操作(順序就是呈現(xiàn)的上下順序)
[alertDialog addAction:laterAction];
[alertDialog addAction:okAction];
// 呈現(xiàn)警告視圖
[self presentViewController:alertDialog animated:YES completion:nil];
}