再談App的版本更新
阿新 • • 發佈:2019-01-06
版本更新的檢測與提示的相關文章已經很多了,但是多數只能應對基本的產品需求。
我們在實際的專案開發過程中,為了更好的使用者體驗和產品升級,有時候我們希望使用者及時的更新,但是這種更新使用者是可選的;但是如果我們遇到一個比較嚴重的漏洞,正在被惡意攻擊的時候,我們希望將所有產品全部更新升級,避免損失,這個時候就需要採取強制更新(不更新軟體則無法繼續使用)的方法。
所以我們需要知道如果有新版本的時候,這個版本是選擇性升級還是強制更新。也就是說,我們只是獲取到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;
}
}