iOS版本更新
2016-10-11 本文已影响893人
卖报的小画家Sure
一、获取app线上版本
苹果提供网址:https://itunes.apple.com/lookup
网址所需参数:id=app对应id(可在iTunes Connect上查看)
请求方式这里使用的为自封装的网络请求类,可以替换为自己的或者改成AFNet等请求亦可
- (void)versionButtonClick{
//此id为你自己项目的标示,可在iTunes Connect上查看
NSDictionary *parameter = @{@"id":@"项目id"};
[HttpRequest get_StartRequestFromUrl:@"https://itunes.apple.com/lookup" AndParameter:parameter returnData:^(NSData *resultData, NSError *error) {
if (!error) {
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:resultData options:NSJSONReadingMutableContainers error:nil];
if ([dict[@"results"]lastObject]) {
NSLog(@"线上版本号:%@",[dict[@"results"]lastObject][@"version"]);
[self checkAppUpdate:[dict[@"results"]lastObject][@"version"]];
}
}
}];
}
二、线上版本与本地版本进行比较。
获取本地版本这里可以在当前程序的info.plist中查找CFBundleShortVersionString即可。
- (void)checkAppUpdate:(NSString *)onlineVersion{
NSString * localVersion = [[[NSBundle mainBundle]infoDictionary]objectForKey:@"CFBundleShortVersionString"];
NSLog(@"本地版本号:%@",localVersion);
/*
因版本号形式为1.0.0、2.1.0等样式,为了便于比较,执行字符串操作,将字符串中的点换为空
例如线上版本为2.1.0 本地版本为2.0.0
进行对应转化后即可变为210与200,如此进行比较会更方便。
*/
NSString * online = [onlineVersion stringByReplacingOccurrencesOfString:@"." withString:@""];
NSString * local = [localVersion stringByReplacingOccurrencesOfString:@"." withString:@""];
if ([online integerValue] > [local integerValue]) {
NSLog(@"有新版本");
}
}