糖糖的iOS专题Swift编程

版本的更迭

2016-07-08  本文已影响92人  我的梦想之路

版本迭代代码

通过OPenURL直接下载

//版本检查
- (void)initVersion
{
/// 通过路径得到服务器最新的版本信息
    NSString *url = @"http://121.40.253.23:8088/dn01/20160622/version.json";
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
/// get请求网络数据
        AFHTTPSessionManager *request = [[AFHTTPSessionManager alloc] init];
        [request GET:url parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSLog(@"=== %@ ",responseObject);
/// 获取网络数据,记录新版本信息(根据你服务器的内容来定)(后文有输出日志)
            _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"]];
            }
/// 一行一行的显示版本更新内容(后文有输出日志)
            NSString *str = [descArray componentsJoinedByString:@"\n"];
            
            /// 判断版本
            if (version > versionCode || version ==versionCode)
            {
                
            }
            else
            {
                UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"发现新版本,是否更新?" message:str preferredStyle: UIAlertControllerStyleAlert];
                UIAlertAction *alertActionOK = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
/// 根据URL(苹果针对于企业证书打包提供的更新路径:@"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,需要一个证书(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);
        }];
    });
}

服务器获取的版本信息日志输出

{
    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";

/// 服务器版本号,与本地进行比较
                versionCode = "1.013";  

/// 一行一行的显示版本更新内容
                versionDesc =                 (
                                        {
                        desc = "1 \U7f3a\U9677\U4fee\U6539";
                    },
                                        {
                        desc = "2 \U6dfb\U52a0\U603b\U76d1\U7248\U672c";
                    }
                );
                versionName = "1.013";
            };
        }
    );
} 

企业证书打包比较麻烦,涉及到申请证书和打包选项,下次更新,静待



打包,流程:http://ask.dcloud.net.cn/article/152

(老详细了,不信,你去试一试就知道了,前提你得找到个翻译老师。)

上一篇下一篇

猜你喜欢

热点阅读