您的位置:首页 > 移动开发 > IOS开发

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];

}


如有纰漏,欢迎指出
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios