博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS 检测版本更新
阅读量:7141 次
发布时间:2019-06-28

本文共 4295 字,大约阅读时间需要 14 分钟。

  这是我在项目中关于版本更新的做法,先说一下大体思路。

  思路:在程序启动时从服务器获取当前最新版本号,并与本地版本号进行比较。

  1.若需要强制更新则只提供更新选项。

  2.若是常规更新则提供“是”、“否”,若用户点击否,则在本地记录获取的版本号,若没有新的版本迭代,以后都不在提示更新

  代码如下:

+ (void)checkVersionWithBlock:(void (^)(NSString *))theBlock {    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:CheckVersionURLString]];        [self postDataWithPath:request  withUseActivityIndicator:NO         withResponseBlock:^(id result, NSError *err) {                          //1.获取当前版本号             NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];                          NSString *appVersion = [infoDictionary objectForKey:@"CFBundleShortVersionString"];             if (result) {                                                  NSString *newVersion = result[@"version"];                                  BOOL isNeedForceUpdate = NO;                 BOOL isNeedUpdate = NO;                           //判断需要强制更新、普通更新           //你的判断代码                 if (isNeedForceUpdate) {
//强制更新 [[WKAlertView shareInstance] showTitle:@"更新提示" withMessage:messageStr withCancelButton:nil withOtherButton:@"更新" withMessageAlignmentLeft:YES withHanderBlock:^(UIAlertView *alertView, NSInteger buttonIndex) { if (buttonIndex == 0) {
//是 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UpdateVersionURLString]]; } }]; [[NSUserDefaults standardUserDefaults] setObject:@(isNeedForceUpdate) forKey:@"isNeedForceUpdate"]; }else if (isNeedUpdate){
//提示更新 //上次检测更新的版本号 //此次检测更新的版本号 //是否取消过 NSString *lastChekedVersion = [userDefaults stringForKey:LastCheckedVersionKey]; BOOL isNeedShowAlert; if(![newVersion isEqualToString:lastChekedVersion]){
//需要显示 isNeedShowAlert = YES; }else{ isNeedShowAlert = NO; } if (isNeedShowAlert) { [[WKAlertView shareInstance] showTitle:@"更新提示" withMessage:messageStr withCancelButton:@"否" withOtherButton:@"是" withMessageAlignmentLeft:YES withHanderBlock:^(UIAlertView *alertView, NSInteger buttonIndex) { if (buttonIndex == 1) {
//是 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UpdateVersionURLString]]; } if (buttonIndex == 0) {
//否 //存储上一次检测更新的版本串 [userDefaults setValue:newVersion forKey:LastCheckedVersionKey]; } }]; [[NSUserDefaults standardUserDefaults] setObject:@(isNeedUpdate) forKey:@"isNeedUpdate"]; [[NSUserDefaults standardUserDefaults]synchronize]; } } } }];}

 

转载于:https://www.cnblogs.com/pretty-guy/p/4548987.html

你可能感兴趣的文章
微信小程序view文本不换行
查看>>
菜鸟学python之类和对象
查看>>
intel或amd,vmware开启支持kvm虚拟化
查看>>
为什么选择开源?企业拥抱开源的十大优势
查看>>
四月份新增开源项目:撩萌妹子必备软萌颜文字生成工具
查看>>
Linux 标准目录结构
查看>>
【小松教你手游开发】【unity实用技能】NGUI Scrollview的Reposition的几个
查看>>
城市供热管网无线监测系统方案
查看>>
百晓生带你玩转linux系统服务搭建系列-----Yum仓库的搭建
查看>>
在AndroidStudio中如何显示出依赖库的本地存储路径
查看>>
面向对象主要有四大特性
查看>>
Spring Cloud:使用Ribbon实现负载均衡详解(上)
查看>>
多线程 NSLock 数据加锁
查看>>
core java 详解附练习题
查看>>
大云之下-----新技术下的网络变革
查看>>
字符画工具
查看>>
vSphere Network Teaming Options
查看>>
Install and Configure OpenStack Identity Service (Keystone) for Ubuntu 14.04
查看>>
我的友情链接
查看>>
Chapter 13 Strings and Regular Expressions
查看>>