iOS版本更新提示
2016-07-20 14:43
393 查看
#pragma mark - 版本检测 - (void)getVersion{ if (AppStore) { // AppStore 更新检测 [self appStoreUpdateVersion]; }else{ // 公司内测版本更新检测 [self firimUpVersion]; } } // AppStore - (void)appStoreUpdateVersion{ // 获取手机程序的版本号 NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]; AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json",@"text/json",@"text/javascript",@"text/html", nil]]; // post 必须上传字段 NSDictionary *dic = @{@"id":@"你的appID"}; [manager POST:@"http://itunes.apple.com/lookup" parameters:dic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { NSArray *array = responseObject[@"results"]; if (array.count != 0) { // 先判断返回的数据是否为空 NSDictionary *dictionary = array[0]; NSLog(@"版本更新 = %@",dictionary); // 判断版本 NSString *appStoreVersion = dictionary[@"version"]; NSLog(@"%f----%f",[self systemVersionCovertToFloat:version],[self systemVersionCovertToFloat:appStoreVersion]); // 通过版本号将版本号转换为float类型,再进行判断大小 if ([self systemVersionCovertToFloat:version] < [self systemVersionCovertToFloat:appStoreVersion]) { // 显示提示框 [self showVersionUpdate:appStoreVersion]; } } } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { NSLog(@"error = %@",error.debugDescription); }]; } #pragma mark - fir.im 的版本号判断 - (void)firimUpVersion{ // 调用后台接口获取版本 NSString *idUrlString = @"http://api.fir.im/apps/latest/此处接fir.im的应用id"; AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; [manager GET:idUrlString parameters:@{@"api_token":@"fir.im的用户token值"} progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { NSLog(@"response= %@",responseObject); NSDictionary *dic = responseObject; // 后台获取的版本号 int newVersion = [dic[@"version"] intValue]; // 当前的版本的build号 int nowVersion = [[CustomFountion getVersion1] intValue]; // 通过 build号进行判断 if (newVersion > nowVersion) { firimURL = dic[@"update_url"]; [self showVersionUpdate:dic[@"versionShort"]]; } } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { NSLog(@"error = %@",error.debugDescription); }]; } // 将版本号 1.1.3 类型的转换成float类型 - (CGFloat )systemVersionCovertToFloat:(NSString *)sVersion{ // NSString *sVersion = [[UIDevice currentDevice] systemVersion]; NSRange fRange = [sVersion rangeOfString:@"."]; CGFloat version = 0.0f; if(fRange.location != NSNotFound){ sVersion = [sVersion stringByReplacingOccurrencesOfString:@"." withString:@""]; NSMutableString *mVersion = [NSMutableString stringWithString:sVersion]; [mVersion insertString:@"." atIndex:fRange.location]; version = [mVersion floatValue]; }else { // 版本应该有问题(由于ios 的版本 是7.0.1,没有发现出现过没有小数点的情况) version = [sVersion floatValue]; } return version; } #pragma mark - 版本更新提示框 - (void)showVersionUpdate:(NSString *)versionStr{ versionStr = [NSString stringWithFormat:@"检测到新版本:%@",versionStr]; // 弹出提示框 UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"软件更新提示" message:versionStr preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *ignoreAction = [UIAlertAction actionWithTitle:@"稍后更新" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { NSLog(@"ignore"); }]; [alertController addAction:ignoreAction]; UIAlertAction *updateAction = [UIAlertAction actionWithTitle:@"立即更新" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { if (AppStore){ // 推送到AppStore “id” 字符不能缺少 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"itms-apps://itunes.apple.com/app/id你的appid"]]; }else{ // 推送到fir.im 应用下载 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:firimURL]]; } }]; [alertController addAction:updateAction]; [self presentViewController:alertController animated:YES completion:nil]; }
如有纰漏,欢迎指出
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- 讲解iOS开发中基本的定位功能实现
- iOS中定位当前位置坐标及转换为火星坐标的方法
- js判断客户端是iOS还是Android等移动终端的方法
- iOS应用开发中AFNetworking库的常用HTTP操作方法小结
- iOS应用中UISearchDisplayController搜索效果的用法
- iOS App开发中的UISegmentedControl分段组件用法总结
- IOS开发环境windows化攻略
- iOS应用中UITableView左滑自定义选项及批量删除的实现
- iOS中UIAlertView警告框组件的使用教程
- 浅析iOS应用开发中线程间的通信与线程安全问题
- iOS中的UIKeyboard键盘视图使用方法小结
- 检测iOS设备是否越狱的方法
- .net平台推送ios消息的实现方法