美文网首页
APP版本更新提示

APP版本更新提示

作者: 为什么划船不靠桨 | 来源:发表于2017-04-24 18:59 被阅读0次
版本更新提示

本文将针对APP新版本提示为大家介绍两种方法,第一种方法是针对已将上线的APP,第二种针对没有上线的APP.

方式1,这个方法适用于已经上线的APP,因为要用到STOREAPPID.首先讲下要怎么获取STOREAPPID,在mac上打开itunes,在商店中找到自己已经上架的APP,然后在获取中选择复制链接,随便找个地方粘贴,就会得到https://itunes.apple.com/cn/app/%E4%BF%A1%E8%B4%B7%E9%80%9A%E7%90%86%E8%B4%A2-%E4%B8%93%E4%B8%9A%E7%9A%84%E6%8A%95%E8%B5%84%E7%90%86%E8%B4%A2%E5%B9%B3%E5%8F%B0/id**********?mt=8
这里id后面跟着的十位数字就是STOREAPPID了!

1.一定要先配置自己项目在商店的STOREAPPID(这里的STOREAPPID是10位纯数字)
#define STOREAPPID @"********"
** 在视图已经显示的时候,调用检测版本号的方法 **

-(void)viewDidAppear:(BOOL)animated{
  [super viewDidAppear:animated];
  //一句代码实现检测更新(需要在viewDidAppear完成时,再调用该方法。不然在网速飞快的时候,会出现一个bug,就是当前控制器viewDidLoad调用的话,可能当前视图还没加载完毕就需要推出UIAlertAction)
  [self hsUpdateApp];
}
下面这个方法用来检测app更新
-(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:@"https://itunes.apple.com/cn/lookup?id=%@",STOREAPPID]]] returningResponse:nilerror:nil];
  if(response ==nil) {
    NSLog(@"你没有连接网络哦");
    return;
  }
  NSDictionary *appInfoDic = [NSJSONSerialization JSONObjectWithData:responseoptions:NSJSONReadingMutableLeaveserror:&error];
  if(error) {
    NSLog(@"hsUpdateAppError:%@",error);
    return;
  }

  NSArray *array = appInfoDic[@"results"];
  if(array.count<1) {
    NSLog(@"此APPID为未上架的APP或者查询不到");
    return;
}

  NSDictionary *dic = array[0];

  NSString *appStoreVersion = dic[@"version"];
  NSLog(@"当前版本号:%@\n商店版本号:%@",currentVersion,appStoreVersion);

  //设置版本号
  currentVersion = [currentVersion stringByReplacingOccurrencesOfString:@"."withString:@""];

  if(currentVersion.length==2) {

    currentVersion= [currentVersionstringByAppendingString:@"0"];

  }elseif(currentVersion.length==1){

    currentVersion= [currentVersionstringByAppendingString:@"00"];

  }

  appStoreVersion = [appStoreVersion stringByReplacingOccurrencesOfString:@"."withString:@""];

  if(appStoreVersion.length==2) {

    appStoreVersion= [appStoreVersion stringByAppendingString:@"0"];

  }elseif(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:UIAlertActionStyleDefaulthandler:^(UIAlertAction*_Nonnullaction) {

      //此处加入应用在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:UIAlertActionStyleCancelhandler:nil];
    [alerc ConteolleraddAction:actionYes];
    [alerc ConteolleraddAction:actionNo];
    [self presentViewController:alerc Conteolleranimated:YES completion:nil];

  }else{

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

    }

}

假如你嫌上面的代码太多太繁琐,而且你的网络请求用是是AFN的话,那么我提供给你一个代码稍微短一点的方式

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager POST:@"https://itunes.apple.com/lookup?id=414478124" parameters:nil success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        NSArray *array = responseObject[@"results"];
        NSDictionary *dict = [array lastObject];
        NSLog(@"当前版本为:%@", dict[@"version"]);
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        [SVProgressHUD showErrorWithStatus:@"请求失败!" ];
    }];

有了新版本,当然要去跟新啦(≧▽≦)/啦啦啦

//去更新
-(void)updataAction{
    //此处加入应用在app store的地址,方便用户去更新,一种实现方式如下
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"itms-apps://itunes.apple.com/app/id%@", STOREAPPID]];
    [[UIApplication sharedApplication] openURL:url];
    
}

接下来介绍第二种方式,其实第二种方式和第一种很类似,第一种是利用的APPstoreid,而第二种是针对没有上线的APP,所以没有App Storeid,那我们用什么呢?其实我们是利用的bundleId.具体看demo

NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary];
NSString *bundleId = infoDict[@"CFBundleIdentifier"];
NSURL *URL = [NSURL URLWithString:[NSString stringWithFormat:@"http://itunes.apple.com/lookup?bundleId=%@",bundleId]];
这样,我们就已经把链接得到了,剩下的步骤就和第一种方式完全相同了,相信聪明的你已经看懂了吧,如果还没看懂,可以在简信和我联系~~~~

相关文章

  • 提示app 版本更新

    开发中我们可能会遇到这样的需求,当AppStore中有新版本迭代更新,在用户点开APP的时候弹框提醒客户去AppS...

  • app提示版本更新

    下面的 sender[@"version"] 就是获取的版本号 注意是String类型的 2 . 比较appSto...

  • APP版本更新提示

    本文将针对APP新版本提示为大家介绍两种方法,第一种方法是针对已将上线的APP,第二种针对没有上线的APP. 方式...

  • APP 版本更新提示

    //版本检查 (void)checkAppVersion{AFHTTPSessionManager * manag...

  • App提示更新bug

    问题描述:App提示更新,但是等更新以后发现实际根本没更新,还是原先的版本,App还是在提示更新,结果继续更新,等...

  • 上架APP进行版本升级检测

    文章来源:iOS-App版本更新提示AppDelegate.m文件: 提示更新的界面增加:

  • 菜鸟教程——实现一句代码实现app更新检测

    版本更新提示是app必备功能,它可以有效提示用户更新。常用的更新提示无非有两种。一种是从苹果api获取版本信息,提...

  • ionic版本更新

    问题描述:启动APP时检测是否有新版本需要更新,提示进行更新 解决思路: 1.从服务器获取版本号,与当前app版本...

  • QQ等APP版本更新(iOS)

    每个APP都会涉及版本更新的问题,所以APP每次启动有新版本的话都会提示版本更新,和新功能 下面我们来看一下效果图...

  • 不知道大家对更新怎么看?到底需不需要每次都要更新啊?

    手机经常提示要更新,版本要升级,我瞟了一眼,该干该就去干啥,懒得更新。 以前,不论哪个app提示更新,我就赶紧更新...

网友评论

      本文标题:APP版本更新提示

      本文链接:https://www.haomeiwen.com/subject/kpwpzttx.html