iOS实现弹窗提示用户升级版本

前言

1.项目成功对接银行存管已经过去几个月,目前线上都还稳定,暂未出现什么问题。今天无意中看到弹窗提示用户更新功能,我们app因对接存管之后,需发一个过渡版,因此也用到了提示用户强制升级功能。
2.AppStore官方审核不允许app界面有任何有关提示更新升级的字样
3.实现后效果


”目前合力贷已经正式上线存管系统,请升级到最新版APP,您也可以暂时通过m站或pc端进行相应操作。“为服务器返回的信息
新版本:当用户点击新版本,则会跳转到appstore相应页面升级更新
去m站:用safri打开我们移动端h5页面
取消:退出应用

主要实现思路:

1.客户端发送更新请求(不用带参数,已通过设置AFNetworking user-agent告知服务器 app Version);
2.判断请求结果,若update为true则弹出升级提示框,否则不处理。(update为true还是false,服务器会根据客户端给的version和数据库最新版本来做比较)


更新接口返回的数据

设置AFNetworking user-agent代码:

AFHTTPSessionManager *session = [AFHTTPSessionManager manager];
    //设置自定义代理参数
[session.requestSerializer setValue:[StaticTools setUserAgentWithParam:@"afn"] forHTTPHeaderField:@"User-Agent"];

#pragma -mark User-Agent添加自定义参数
+ (NSString *)setUserAgentWithParam:(NSString *)param{
    NSMutableString *newAgent;
    UIWebView *webView = [[UIWebView alloc] init];
    NSString *userAgent = [webView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];
    
    NSString *version_current = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
    newAgent = [NSMutableString stringWithString:userAgent];
    //查找Helloan_IOS_APP字符串
    NSRange substr = [newAgent rangeOfString:@"Helloan_IOS_APP"];
    if (substr.location != NSNotFound) {
        //有这个字符串
    }else{
        //没有的话追加
        [newAgent appendFormat:@"%@%@", @" Helloan_IOS_APP/",version_current];
    }
    NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:newAgent, @"UserAgent", nil];
    [[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];
    
    if ([param isEqualToString:@"web"]) {
        //加载web页面
        return nil;
    }else{
        return newAgent;
    }
}

发送更新请求及判断代码:

- (void)reqUpdateNotice{
    [[PersonalCenterLogicManager sharedInstance] reqUpdateNoticeWithSuccess:^(id model) {
        NSLog(@"reqUpdateNoticeWithSuccess---%@", model);
        
        if ([[model valueForKey:@"code"] isEqualToString:Response_OK]) {
            UpdateNoticeModel *dataModel = [UpdateNoticeModel modelWithDic:[model valueForKey:@"data"]];
            if (dataModel.update) {
                UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"升级提示" message:dataModel.msg preferredStyle:(UIAlertControllerStyleAlert)];
                UIAlertAction *action = [UIAlertAction actionWithTitle:@"新版本" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction *action) {
                    //跳转到appstore
                    NSURL* url = [NSURL URLWithString:AppstoreUrlString];
                    if([[UIApplication sharedApplication]canOpenURL:url]){
                        [[UIApplication sharedApplication] openURL:url];
                    }
                    else{
                        NSLog(@"can not open");
                    }
                }];
                UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                    //退出应用
                    exit(0);
                }];
                UIAlertAction *gotoM = [UIAlertAction actionWithTitle:@"去m站" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    //去m站
                    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:URL_M]];
                }];
                [alert addAction:action];
                [alert addAction:cancel];
                [alert addAction:gotoM];
                [self.window.rootViewController presentViewController:alert animated:NO completion:nil];
            }
        }
    } failure:^(NSError *err) {
        
    }];
}

如何获取AppStore地址?

https://itunes.apple.com/cn/app/id964419514?mt=8
只需要把链接中的id号换成自己的就可以了。这个是中文版的。
还可以在ituns中获取。ituns中获取,参考链接

或itunes获取app路径,具体步骤如下:


如上图得到app链接,发现是这样的
https://itunes.apple.com/cn/app/%E5%90%88%E5%8A%9B%E8%B4%B7%E7%90%86%E8%B4%A2-%E5%80%BC%E5%BE%97%E4%BF%A1%E8%B5%96%E7%9A%84p2p%E6%8A%95%E8%B5%84%E7%90%86%E8%B4%A2%E7%A5%9E%E5%99%A8/id964419514?mt=8

没关系把编码的部分删掉,剩下如下部分:
https://itunes.apple.com/cn/app/id964419514?mt=8
ok。

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

推荐阅读更多精彩内容