iOS 版本更新
當在APP在前臺的時候(這個時間點自定義)對本地版本和線上版本做比較犀被,若本地的版本低于線上的版本噩凹,則需要進行版本更新侧啼,然后彈出提示框提示用戶去APP Store版本更新巍实,說下自己的實現(xiàn)携茂。
打算做這倆種 一種是必須你得點去APP Store看(更不更看用戶了....) 另一種是你可以不去App Store看 以簡書為例
1.檢查更新就是請求線上的版本號與線下的版本號比較呛占,自己封裝的類名叫XDUpdateManager
通過plist 文件獲取應(yīng)用當前本地版本
NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
self.locVersion= [infoDictionary objectForKey:@"CFBundleShortVersionString"];
獲取網(wǎng)上的版本請求地址 APPID為創(chuàng)建項目的唯一編碼
self.appStoreUrl=[[NSString alloc] initWithFormat:@"http://itunes.apple.com/lookup?id=%@",APPID];
如果你在App Store找了個APPID 但發(fā)現(xiàn)沒有跳轉(zhuǎn)到相應(yīng)位置 試試在路徑加國家 (/cn中國)
self.appStoreUrl=[[NSString alloc] initWithFormat:@"https://itunes.apple.com/cn/lookup?id=%@",APPID];
版本比較的方法
/**
版本比較方法
@param versionOne 線上版本
@param versionTwo 本地項目版本
@return 比較結(jié)果
*/
- (NSComparisonResult)compareOnlineVersion:(NSString*)versionOne toVersion:(NSString*)versionTwo {
NSArray* versionOneArr = [versionOne componentsSeparatedByString:@"."];
NSArray* versionTwoArr = [versionTwo componentsSeparatedByString:@"."];
NSInteger pos = 0;
while ([versionOneArr count] > pos || [versionTwoArr count] > pos) {
NSInteger v1 = [versionOneArr count] > pos ? [[versionOneArr objectAtIndex:pos] integerValue] : 0;
NSInteger v2 = [versionTwoArr count] > pos ? [[versionTwoArr objectAtIndex:pos] integerValue] : 0;
if (v1 < v2) {
//版本2大
return NSOrderedAscending;
}
else if (v1 > v2) {
//版本1大
return NSOrderedDescending;
}
pos++;
}
//版本相同
return NSOrderedSame;
}
檢查更新方法
/**
檢查更新方法
*/
+(void)CheckVersionUpadateWithForce:(BOOL)isForce{
XDUpdateManager *manager=[[XDUpdateManager alloc]init];
[[AFHTTPSessionManager manager]GET:manager.appStoreUrl parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
//判斷是否有結(jié)果
if(responseObject[@"resultCount"]>0){
//取出線上的版本號
NSString *onlineVersion = [responseObject[@"results"] firstObject][@"version"];
//獲取更新版本信息
manager.updateMessage = [responseObject[@"results"] firstObject][@"releaseNotes"];
switch ([manager compareOnlineVersion:onlineVersion toVersion:manager.locVersion]) {
//線上的版本小 不做操作
case -1:
{
}
break;
//版本相同 不做操作
case 0:
{
//也許是app更新完成后 需要清空之前取消次數(shù)
[manager clearPlistChannelCount];
}
break;
//線上的版本大 說明本地要進行更新操作
case 1:
{ //是否強制更新
if(isForce){
[manager showAlert:YES];
}
//帶取消按鈕更新彈窗
else{
[manager showAlert:NO];
}
}
break;
default:
break;
} ;
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//失敗蘋果服務(wù)器掛了 不做操作
}];
}
2.判斷完成之后就是要進行彈窗了 其中實現(xiàn)了記錄點擊取消更新的次數(shù) 若超過次數(shù)則不在彈出更新窗口了 若app更新完還需重置app取消次數(shù)
/**
更新彈窗
@param isforce 是否為強制彈窗
*/
-(void)showAlert:(BOOL)isforce{
UIAlertController *alertVc=[UIAlertController alertControllerWithTitle:UPDATEMESSEGE message:nil preferredStyle:UIAlertControllerStyleAlert];
UIViewController *cv=[self getCurrentVC];
UIAlertAction *ok = [UIAlertAction actionWithTitle:UPDATEOK style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
//跳轉(zhuǎn)到appStore 須真機測試看效果 適配8系統(tǒng)用這個老的跳轉(zhuǎn)
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://itunes.apple.com/app/id%@?mt=8", APPID]]];
//點擊后還要重新彈出 始終在app視圖上顯示
if (isforce) {
[self showAlert:YES];
}
}];
UIAlertAction *cancel = [UIAlertAction actionWithTitle:UPDATECHANNEL style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
//異步操作吧 文件寫入有點費時
dispatch_async(dispatch_get_global_queue(0, 0), ^{
//增加取消次數(shù)
[self ChannelCountAdd];
});
}];
if (isforce) {
//添加按鈕
[alertVc addAction:ok];
[cv presentViewController:alertVc animated:YES completion:nil];
}
else{
dispatch_async(dispatch_get_global_queue(0, 0), ^{
//這個需要文件創(chuàng)建讀寫費時就異步啦
NSInteger chanelCount=[self getChannelCount];
dispatch_async(dispatch_get_main_queue(), ^{
//看是否彈出最大次數(shù) 彈出窗體
if (chanelCount<MAXCHANNELCOUNT) {
//添加按鈕
[alertVc addAction:ok];
[alertVc addAction:cancel];
[cv presentViewController:alertVc animated:YES completion:nil];
}
});
});
}
}
/**
版本更新把取消更新次數(shù)重置方法
*/
-(void)clearPlistChannelCount{
//先判斷plist是否存在 不存在可以先不考慮 等有更新在創(chuàng)建文件就可以
if ([[NSFileManager defaultManager] fileExistsAtPath:self.updatePlistPath]) {
//判斷plist文件中存的版本
NSString *plistVersion=[NSDictionary dictionaryWithContentsOfFile:self.updatePlistPath][@"plistVersion"];
//如果plist文件中版本不同則更新plist文件中版本號并清空取消次數(shù)
if (![plistVersion isEqualToString:self.locVersion]) {
NSMutableDictionary *updateDic=[NSMutableDictionary dictionaryWithContentsOfFile:self.updatePlistPath];
[updateDic setValue:@(0) forKey:@"channelCount"];
[updateDic setValue:self.locVersion forKey:@"plistVersion"];
[updateDic writeToFile:self.updatePlistPath atomically:YES];
}
}
}
可以通過宏來設(shè)置更新彈窗的各種所需參數(shù)
//本app的唯一識別碼(這個碼我去iTunes找的簡書的現(xiàn)在是3.4.1) 跳轉(zhuǎn)appStore需要真機
#define APPID @"888237539"
//設(shè)置點擊取消更新的總共展示次數(shù) 超過次數(shù)就不在提示更新版本了
#define MAXCHANNELCOUNT 5
//彈窗的展示標題
#define UPDATEMESSEGE @"有新版本了"
//彈窗的確定按鈕文字
#define UPDATEOK @"去更新"
//彈窗的取消按鈕文字
#define UPDATECHANNEL @"暫不更新"
3.使用方法(在想要進行判斷提示地方加入這句話孤钦,yes為一個按鈕的,no為倆按鈕的)
[XDUpdateManager CheckVersionUpadateWithForce:YES];
4.附上Demo地址紧武。