前言扯淡:
那是2015年的第一场演唱会,库克说不让你们应用里有自动更新的按钮了,遂之后的怎么更新呢,我们还是会看到有的应用汇自动更新。所谓道高一尺,就来入魔吧
一朵&温柔.av
目前市面上貌似有很多,自动更新,自动化参数的平台;谁谁谁,就那几个谁。
上面是扯淡,下面是正文
正文:
#pragma -mark 天朝强制升级版
-(void)putYoHandsUp{
//2先获取当前工程项目版本号
NSDictionary *infoDic=[[NSBundle mainBundle] infoDictionary];
NSString *currentVersion=infoDic[@"CFBundleShortVersionString"];
NSLog(@"当前版本号%@",currentVersion);
//获得build号:
NSString *strBuild = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
NSLog(@"build号 = %@",strBuild);
//3从网络获取appStore版本号
NSError *error;
//已经被弃用了,不过也可以用NSURLConnection
// NSData *response = [NSURLConnection sendSynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString returningResponse:nil error:nil];
// 快捷方式获得session对象
NSURLSession *session = [NSURLSession sharedSession];
NSURL *url = [NSURL URLWithString:[NSString
// 通过URL初始化task,在block内部可以直接对返回的数据进行处理
NSURLSessionTask *task = [session dataTaskWithURL:url
completionHandler:^(NSData *data, NSURLResponse *response, NSError* error) {
// NSLog(@"00000000%@", response);
responseData = data;
}];
// 启动任务
[task resume];
if (responseData == nil) {
NSLog(@"你没有连接网络哦");
return;
}
NSDictionary *appInfoDic = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves error:&error];
if (error) {
NSLog(@"hsUpdateAppError:%@",error);
return;
}
NSArray *array = appInfoDic[@"results"];
NSDictionary *dic = array[0];
NSString *appStoreVersion = dic[@"version"];
//打印版本号
NSLog(@"当前版本号:%f\n商店版本号:%f",[currentVersion doubleValue],[appStoreVersion doubleValue]);
//4当前版本号小于商店版本号,就更新
if([currentVersion doubleValue] < [appStoreVersion doubleValue])
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"dd" message:@"ddd" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:nil, nil];
[alert show];
}else{
NSLog(@"版本号好像比商店大噢!检测到不需要更新");
}
}
TIPS:
没有觉得拿去直接用吗,看官顺手点个赞啦(≧▽≦)/-。- 注意,这里有一些细节,如果要是用版本号的话,版本号的书写要有规则,不要写类似于“1.2.1”这种的,因为在转换成float or double时候,直接会变成1.20;其中的意思我相信你懂的。