检测版本号更新操作

1.一定要先配置自己项目在商店的APPID(从苹果开发者中心登录自己的账号可看),配置完最好在真机上运行才能看到完全效果哦!

define STOREAPPID @"1104867082" // 宏定义APPID

/**

  • 天朝专用检测app更新
    一句代码实现检测更新,很简单哦 (需要在viewDidAppear完成时,再调用改方法。不然 在网速飞快的时候,会出现一个bug,就是当前控制器viewDidLoad调用的话,可能当前视 图还没加载完毕就需要推出UIAlertAction)
    */
    -(void)hsUpdateApp
    {
    //2先获取当前工程项目版本号
    NSDictionary *infoDic=[[NSBundle mainBundle] infoDictionary];
    NSString *currentVersion=infoDic[@"CFBundleShortVersionString"];

    //3从网络获取appStore版本号
    NSError *error;
    NSData *response = [NSURLConnection sendSynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://itunes.apple.com/cn/lookup?id=%@",STOREAPPID]]] returningResponse:nil error:nil];
    if (response == nil) {
    NSLog(@"你没有连接网络哦");
    return;
    }
    NSDictionary *appInfoDic = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];
    if (error) {
    NSLog(@"hsUpdateAppError:%@",error);
    return;
    }
    // NSLog(@"%@",appInfoDic);
    NSArray *array = appInfoDic[@"results"];
    NSDictionary *dic = array[0];
    NSString *appStoreVersion = dic[@"version"];
    //打印版本号
    NSLog(@"当前版本号:%@\n商店版本号:%@",currentVersion,appStoreVersion);
    //设置版本号
    currentVersion = [currentVersion stringByReplacingOccurrencesOfString:@"." withString:@""];
    if (currentVersion.length==2) {
    currentVersion = [currentVersion stringByAppendingString:@"0"];
    }else if (currentVersion.length==1){
    currentVersion = [currentVersion stringByAppendingString:@"00"];
    }
    appStoreVersion = [appStoreVersion stringByReplacingOccurrencesOfString:@"." withString:@""];
    if (appStoreVersion.length==2) {
    appStoreVersion = [appStoreVersion stringByAppendingString:@"0"];
    }else if (appStoreVersion.length==1){
    appStoreVersion = [appStoreVersion stringByAppendingString:@"00"];
    }

    //4当前版本号小于商店版本号,就更新
    if([currentVersion floatValue] < [appStoreVersion floatValue])
    {
    UIAlertController *alercConteoller = [UIAlertController alertControllerWithTitle:@"版本有更新" message:[NSString stringWithFormat:@"检测到新版本(%@),是否更新?",dic[@"version"]] preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *actionYes = [UIAlertAction actionWithTitle:@"更新" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    //此处加入应用在app store的地址,方便用户去更新,一种实现方式如下
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://itunes.apple.com/us/app/id%@?ls=1&mt=8", STOREAPPID]];
    [[UIApplication sharedApplication] openURL:url];
    }];
    UIAlertAction *actionNo = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {

    }];
    [alercConteoller addAction:actionYes];
    [alercConteoller addAction:actionNo];
    [self presentViewController:alercConteoller animated:YES completion:nil];
    

    }else{
    NSLog(@"版本号好像比商店大噢!检测到不需要更新");
    }

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 有些App更新新功能并且上线后,会有这样的需求:用户登录后,会提示用户“发现新版本,请更新”,这就需要用户点击确定...
    海耐射手阅读 5,294评论 4 2
  • 做iOS开发App很久了,今天想写写博客,把自己的一些想法、技术点、遇到问题写出来,供大家参考; app版本更新是...
    奋斗ing0310阅读 1,171评论 0 2
  • //生成属性 当前版本 和 appStore上的版本 @property (nonatomic, strong) ...
    Hope_Man阅读 208评论 0 0
  • 1、禁止手机睡眠[UIApplication sharedApplication].idleTimerDisabl...
    DingGa阅读 1,152评论 1 6
  • 西安的天气最近变得很好,一场雪刷走了停留很久的雾霾,空气里冷冰冰的,但是很清新,阳光也温柔灿烂,暖暖地笼罩我。 一...
    浅其阅读 374评论 3 3