版本迭代代碼
通過OPenURL直接下載
//版本檢查
- (void)initVersion
{
/// 通過路徑得到服務(wù)器最新的版本信息
NSString *url = @"http://121.40.253.23:8088/dn01/20160622/version.json";
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
/// get請求網(wǎng)絡(luò)數(shù)據(jù)
AFHTTPSessionManager *request = [[AFHTTPSessionManager alloc] init];
[request GET:url parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"=== %@ ",responseObject);
/// 獲取網(wǎng)絡(luò)數(shù)據(jù)九秀,記錄新版本信息(根據(jù)你服務(wù)器的內(nèi)容來定)(后文有輸出日志)
_versionDic = [[[responseObject objectForKey:@"version"] objectAtIndex:1] objectForKey:@"ios"];
NSString *versionCodeStr = [_versionDic objectForKey:@"versionCode"];
float versionCode = [versionCodeStr floatValue];
NSLog(@"_versionCode %lf",versionCode);
//// 獲取本地版本號(方法不唯一)
NSString *versionStr = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
float version = [versionStr floatValue];
NSLog(@"version %lf ",version);
NSMutableArray *descArray = [NSMutableArray array];
NSArray *versionDesc = [_versionDic objectForKey:@"versionDesc"];
for (int i = 0; i < [versionDesc count]; i ++) {
[descArray addObject:[[versionDesc objectAtIndex:i] objectForKey:@"desc"]];
}
/// 一行一行的顯示版本更新內(nèi)容(后文有輸出日志)
NSString *str = [descArray componentsJoinedByString:@"\n"];
/// 判斷版本
if (version > versionCode || version ==versionCode)
{
}
else
{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"發(fā)現(xiàn)新版本,是否更新?" message:str preferredStyle: UIAlertControllerStyleAlert];
UIAlertAction *alertActionOK = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
/// 根據(jù)URL(蘋果針對于企業(yè)證書打包提供的更新路徑:@"itms-services://?action=download-manifest&url=%@")后面拼接新版本plist文件URL(下面有日志輸出)
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:[NSString stringWithFormat:@"itms-services://?action=download-manifest&url=%@",[_versionDic objectForKey:@"updateUrl"]]]];
}];
#如上奢啥,拼接的updateUrl必須支持https第喳,需要一個(gè)證書(CE飞蚓?EA)沒記清楚
UIAlertAction *alertActionCancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}];
[alertController addAction:alertActionOK];
[alertController addAction:alertActionCancel];
[self presentViewController:alertController animated:YES completion:nil];
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"請求失敗 %@",error);
}];
});
}
服務(wù)器獲取的版本信息日志輸出
{
version = (
{
android = {
forceUpdate = 0;
updateUrl = "http://121.40.253.23:8088/dn01/20160622/rse.apk";
versionCode = 4;
versionDesc = (
{
desc = "1 \U7f3a\U9677\U4fee\U6539";
},
{
desc = "2 \U6dfb\U52a0\U603b\U76d1\U7248\U672c";
}
);
versionName = "1.0.4";
};
},
{
ios = {
forceUpdate = 0;
/// 新版本plist文件URL
updateUrl = "https://dn-ceic.qbox.me/rse.plist";
/// 服務(wù)器版本號,與本地進(jìn)行比較
versionCode = "1.013";
/// 一行一行的顯示版本更新內(nèi)容
versionDesc = (
{
desc = "1 \U7f3a\U9677\U4fee\U6539";
},
{
desc = "2 \U6dfb\U52a0\U603b\U76d1\U7248\U672c";
}
);
versionName = "1.013";
};
}
);
}