版本更新的检测与提示的相关文章已经很多了,但是多数只能应对基本的产品需求。
先给大家推荐一个封装好的工具类,实现了一行代码检测app是否有新版本,代码很简单,GitHub地址:https://github.com/wolfhous/HSUpdateApp
我们在实际的项目开发过程中,为了更好的用户体验和产品升级,有时候我们希望用户及时的更新,但是这种更新用户是可选的;但是如果我们遇到一个比较严重的漏洞,正在被恶意攻击的时候,我们希望将所有产品全部更新升级,避免损失,这个时候就需要采取强制更新(不更新软件则无法继续使用)的方法。
所以我们需要知道如果有新版本的时候,这个版本是选择性升级还是强制更新。也就是说,我们只是获取到App Store有新版本是不够用的。这个时候,我们需要通过接口获取版本升级是否强制这个属性,这个属性一定是在app的后台管理中进行设置的,每次app更新的时候都需要进行设置,那么我们可以不用再去App Store获取版本信息,直接让后台管理员在app有新版本的时候在后台进行设置一下版本号和是否强制,在接口中统一获取即可。
还有一个问题,提示用户进行版本升级的时候,我们一般都会和用户展示此次版本更新优化或添加了什么功能,修改了什么bug等,这些也是我们需要从接口中获取的,那么我们就在后台添加一个文本输入框,在接口中传给app端一个网页URL,app端直接调用webView进行展示。
这样,后台管理系统中可以添加一个模块 — 版本更新,里面设置新版本版本号,是否强制升级,更新提示内容。
下面列出的是app端的页面展示一些代码:
@interface ViewController ()<UIWebViewDelegate>
{
CGFloat alertViewWith;
CGFloat webViewDefaultHight;
CGFloat alertViewDefaultHight;
NSDictionary *alertData;
}
@property (nonatomic,strong)UIView *blackView;
@property (strong,nonatomic)UIView * alertview;
@property (strong,nonatomic)UIWebView *webView;
@property (strong,nonatomic)UILabel *alertTitleLabel;
@end
#pragma mark - 弹出框提示更新版本
- (void)addAlertWebViewWithVersion:(NSString *)version {
alertViewWith = SCREENWIDTH -20;
webViewDefaultHight = SCREENHEIGHT/2;
alertViewDefaultHight = SCREENHEIGHT/2;
//背景遮盖
_blackView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT)];
_blackView.backgroundColor = [UIColor blackColor];
_blackView.alpha = 0.5;
[[UIApplication sharedApplication].windows.firstObject addSubview:_blackView];
//创建alert
self.alertview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, alertViewWith, alertViewDefaultHight)];
self.alertview.center = self.view.center;
self.alertview.layer.cornerRadius = 17;
self.alertview.clipsToBounds = YES;
self.alertview.backgroundColor = [UIColor whiteColor];
//设置弹出框
[self setupAlertViewWithVersion:[alertData objectForKey:@"nowbanben"]];
[[UIApplication sharedApplication].windows.firstObject addSubview:_alertview];
[self exChangeOut:self.alertview dur:0.6];
}
-(void)setupAlertViewWithVersion:(NSString *) versionStr{
_alertTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 5, alertViewWith, 44)];
_alertTitleLabel.font = [UIFont systemFontOfSize:18];
_alertTitleLabel.textColor = [UIColor blackColor];
_alertTitleLabel.textAlignment = NSTextAlignmentCenter;
_alertTitleLabel.text = [NSString stringWithFormat:@"有新版本发布%@",versionStr];
[self.alertview addSubview:_alertTitleLabel];
UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, _alertTitleLabel.bottom, alertViewWith, 1)];
lineView.backgroundColor = [UIColor colorWithRed:226/255.0 green:226/255.0 blue:226/255.0 alpha:1];
[self.alertview addSubview:lineView];
_webView = [[UIWebView alloc]initWithFrame:CGRectMake(5, lineView.bottom, alertViewWith - 10, webViewDefaultHight)];
_webView.delegate = self;
[_webView loadHTMLString:[alertData objectForKey:@"remark"] baseURL:[NSURL URLWithString:ImgDuanKou]];
// NSURLRequest *request =[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.baidu.com"]];
// [_webView loadRequest:request];
[self.alertview addSubview:_webView];
}
-(void)chooseClick {
NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary];
NSString *currentVersion = [infoDict objectForKey:@"CFBundleVersion"];
[[NSUserDefaults standardUserDefaults] setObject:currentVersion forKey:@"version"];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:APP_URL]];
}
#pragma mark - alertView 弹出或隐藏
-(void)exChangeOut:(UIView *)changeOutView dur:(CFTimeInterval)dur {
CAKeyframeAnimation * animation;
animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
animation.duration = dur;
animation.removedOnCompletion = NO;
animation.fillMode = kCAFillModeForwards;
NSMutableArray *values = [NSMutableArray array];
[values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)]];
[values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.2, 1.2, 1.0)]];
[values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.9, 0.9, 0.9)]];
[values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
animation.values = values;
animation.timingFunction = [CAMediaTimingFunction functionWithName: @"easeInEaseOut"];
[changeOutView.layer addAnimation:animation forKey:nil];
}
- (void)cancleView{
[UIView animateWithDuration:0.3 animations:^{
self.alertview.alpha = 0.0;
} completion:^(BOOL finished) {
[self.alertview removeFromSuperview];
[self.blackView removeFromSuperview];
self.alertview = nil;
self.blackView = nil;
}];
}
#pragma mark - UIWebViewDelegate
-(void)webViewDidFinishLoad:(UIWebView *)webView{
//计算web加载完后的高度,刷新界面高度
[self changeWebViewHeight:webView.scrollView.contentSize.height];
}
-(void)changeWebViewHeight:(CGFloat)webViewHeight {
CGFloat showHeight = 0;
if (webViewHeight > webViewDefaultHight) {
showHeight = webViewDefaultHight;
}else{
showHeight = webViewHeight;
}
_webView.frame = CGRectMake(_webView.frame.origin.x, _webView.frame.origin.y, _webView.frame.size.width, showHeight);
_alertview.size = CGSizeMake(alertViewWith, showHeight+88);
_alertview.center = self.view.center;
UIView *lineViewSecond = [[UIView alloc]initWithFrame:CGRectMake(0, _webView.bottom, alertViewWith, 1)];
lineViewSecond.backgroundColor = [UIColor colorWithRed:226/255.0 green:226/255.0 blue:226/255.0 alpha:1];
[self.alertview addSubview:lineViewSecond];
UIButton *chooseBtn = [UIButton buttonWithType:UIButtonTypeCustom];
chooseBtn.frame = CGRectMake(alertViewWith/ 2, self.alertview.frame.size.height - 44, alertViewWith/ 2, 44);
[chooseBtn setBackgroundColor:[UIColor whiteColor]];
chooseBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
[chooseBtn setTitle:@"马上更新" forState:UIControlStateNormal];
[chooseBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[chooseBtn addTarget:self action:@selector(chooseClick) forControlEvents:UIControlEventTouchUpInside];
[self.alertview addSubview:chooseBtn];
NSInteger flog = [[alertData objectForKey:@"ismust"] integerValue];
if (flog == 0) {//非必须更新
UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
cancelBtn.frame = CGRectMake(0, self.alertview.frame.size.height - 44, alertViewWith/ 2, 44);
[cancelBtn setBackgroundColor:[UIColor whiteColor]];
cancelBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
[cancelBtn setTitle:@"以后再说" forState:UIControlStateNormal];
[cancelBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[cancelBtn addTarget:self action:@selector(cancleView) forControlEvents:UIControlEventTouchUpInside];
[self.alertview addSubview:cancelBtn];
UIView *lineViewThird = [[UIView alloc]initWithFrame:CGRectMake(alertViewWith / 2 - 1, lineViewSecond.bottom + 5, 1, 30)];
lineViewThird.backgroundColor = [UIColor colorWithRed:226/255.0 green:226/255.0 blue:226/255.0 alpha:1];
[self.alertview addSubview:lineViewThird];
}else{//必须更新
CGRect rect = chooseBtn.frame;
rect.origin.x = 0;
rect.size.width = alertViewWith;
chooseBtn.frame = rect;
}
}