ios实现弹窗提醒用户更新版本

前言

  • 第一次写简书,不好的地方还请担待
  • 现在AppStore官方审核不允许提示更新升级的字样,但是我下面所用的方法可以通过审核,可以放心的使用

版本更新的实现的效果

屏幕快照 2016-12-08 11.30.21.png
  • 我这里就写了一个按钮,所以用户不能取消,如果不升级,每次打开都会有这个弹框
  • 当然也可以设置一个取消的按钮,但是我们公司的要求的相当于强制更新,我只能写一个按钮

版本更新实现的思路

  • 获取自身的版本号
  • 获取AppStore的版本号
  • 自身的版本号和AppStore的比较
  • 弹窗提示

所需数据的获取的方式

1.获取自身的版本号

WechatIMG56.jpeg

2.AppStore的版本号

WechatIMG58.jpeg

具体实现的代码

  • 网络请求app的信息
-(void)VersonUpdate{
    //定义的app的地址
    NSString *urld = [NSString stringWithFormat:@"http://itunes.apple.com/lookup?id=%@",@"你的APPID"];
    
    //网络请求app的信息,主要是取得我说需要的Version
    NSURL *url = [NSURL URLWithString:urld];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
                                                           cachePolicy:NSURLRequestReloadIgnoringCacheData
                                                       timeoutInterval:10];
    [request setHTTPMethod:@"POST"];
    
    NSURLSession *session = [NSURLSession sharedSession];
    
    NSURLSessionDataTask *task = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        NSMutableDictionary *receiveStatusDic=[[NSMutableDictionary alloc]init];
        if (data) {
            
            //data是有关于App所有的信息
            NSDictionary *receiveDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
            if ([[receiveDic valueForKey:@"resultCount"] intValue]>0) {
                
                [receiveStatusDic setValue:@"1" forKey:@"status"];
                [receiveStatusDic setValue:[[[receiveDic valueForKey:@"results"] objectAtIndex:0] valueForKey:@"version"]   forKey:@"version"];
                
                //请求的有数据,进行版本比较
                [self performSelectorOnMainThread:@selector(receiveData:) withObject:receiveStatusDic waitUntilDone:NO];
            }else{
                
                [receiveStatusDic setValue:@"-1" forKey:@"status"];
            }
        }else{
            [receiveStatusDic setValue:@"-1" forKey:@"status"];
        }
    }];
    
    [task resume];
}
  • 获取自身的版本号并与AppStore比较
-(void)receiveData:(id)sender
{
    //获取APP自身版本号
    NSString *localVersion = [[[NSBundle mainBundle]infoDictionary]objectForKey:@"CFBundleShortVersionString"];
    
    NSArray *localArray = [localVersion componentsSeparatedByString:@"."];
    NSArray *versionArray = [sender[@"version"] componentsSeparatedByString:@"."];
    
    
    if ((versionArray.count == 3) && (localArray.count == versionArray.count)) {
        
        if ([localArray[0] intValue] <  [versionArray[0] intValue]) {
            [self updateVersion];
        }else if ([localArray[0] intValue]  ==  [versionArray[0] intValue]){
            if ([localArray[1] intValue] <  [versionArray[1] intValue]) {
                [self updateVersion];
            }else if ([localArray[1] intValue] ==  [versionArray[1] intValue]){
                if ([localArray[2] intValue] <  [versionArray[2] intValue]) {
                    [self updateVersion];
                }
            }
        }
    }
}
  • 根据比较的结果,实现弹窗
-(void)updateVersion{
    NSString *msg = [NSString stringWithFormat:@"更新最新版本,优惠信息提前知"];
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"升级提示" message:msg preferredStyle:UIAlertControllerStyleAlert];
   UIAlertAction *otherAction = [UIAlertAction actionWithTitle:@"现在升级"style:UIAlertActionStyleDestructive handler:^(UIAlertAction*action) {
        
        NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"你的app在商店的下载地址"]];
        [[UIApplication sharedApplication]openURL:url];
    }];
   [alertController addAction:otherAction];
   [self.window.rootViewController presentViewController:alertController animated:YES completion:nil];
    
}

温馨提示

  • 我这种实现的方式是根据自身的版本和线上版本,比较大小来实现的弹窗,如果比线上的版本小,就会弹窗,具体的比较看上面的代码。这样上线可以瞒天过海。
  • 当你想实现提醒更新的时候,直接的把我上面的代码,copy到AppDelegate里面即可,当然还要换上你自己的appid,具体的获取方式,上面已经写明。
  • 如果想模拟器测试效果,只要把version改的比线上的版本小就可以。
  • 我的弹窗比较“简陋”,具体的好看的效果,还需要自己去改变了。
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,094评论 25 709
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,623评论 4 61
  • 高三的时候,全力以赴自己的高考,不曾有一点的怠慢。但是依旧不能抵挡自己去挤出时间阅读一本书——《追风筝的人》。 一...
    温暖的团子爸爸阅读 1,742评论 3 3
  • 台剧荼靡中演绎着平行时空中两种选择,两种人生。良心编剧让故事有了happy ending.没有如果,也不需要懊悔,...
    Elaine等风来阅读 2,929评论 0 0
  • 边疆一词的设定,意味着距离的拉大和控制的减弱,边疆具备了地缘政治的所有特色,既是屏障,也是失控的代名词。 在《寻求...
    Andylee阅读 3,666评论 0 3

友情链接更多精彩内容