(iOS开发)IOS 9 APP版本检验更新和跳转

/**
 *   判断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];

});







  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值