/**
* 判断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];
});
(iOS开发)IOS 9 APP版本检验更新和跳转
最新推荐文章于 2022-10-18 11:30:10 发布