因为项目里需要判断当前版本是否需要更新,可更新范围是一个区间,所以封装了如下方法来判断当前版本是否在更新区间中。(有一个缺陷是更新的版本号的长度不能大于本地版本号长度,否则判断会有遗漏)
通过服务器数据判断本地与远端最新版是否一致
方案1:自己去实现判断逻辑,遍历字段逐一校验
//判断版本号,输出YES(更新) 输出NO(无需更新)
- (BOOL)compareEditionMinNumber:(NSString *)serverMinNumberStr maxNumber:(NSString *)serverMaxNumberStr {
//获取本地的软件版本号
NSString *localNumberStr = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
NSArray *minArray = [serverMinNumberStr componentsSeparatedByString:@"."]; //从字符A中分隔
NSArray *maxArray = [serverMaxNumberStr componentsSeparatedByString:@"."]; //从字符A中分隔
NSArray *localArray = [localNumberStr componentsSeparatedByString:@"."]; //从字符A中分隔
//先判断本地版本和最低更新版本号的逻辑
for (int i=0; i<localArray.count; i++) {
NSString *str = localArray[i];
int localNum = [str intValue];
if (!localNum) {
localNum = 0;
}
if (minArray.count > i) {
int minNum = [minArray[i] intValue];
if (!minNum) {
minNum = 0;
}
if (localNum > minNum) {
//符合预期且不用判断后续小版本号,跳出整个循环
break;
}
if (localNum == minNum) {
//说明符合预期且需要判断后续小版本号,跳出本次循环
continue;
}
if (localNum < minNum) {
//如果小于最小版本号,直接return
return NO;
}
}else{
continue;
}
}
//再判断本地版本和最高更新版本号的逻辑
for (int i=0; i<localArray.count; i++) {
NSString *str = localArray[i];
int localNum = [str intValue];
if (!localNum) {
localNum = 0;
}
if (maxArray.count > i) {
int maxNum = [maxArray[i] intValue];
if (!maxNum) {
maxNum = 0;
}
if (localNum < maxNum) {
//符合预期且不用判断后续小版本号,跳出整个循环
break;
}
if (localNum == maxNum) {
//说明符合预期且需要判断后续小版本号,跳出本次循环
continue;
}
if (localNum > maxNum) {
//如果大于最大版本号,直接return
return NO;
}
}else{
continue;
}
}
//如果循环逻辑都通过了,说明全部字段符合预期
return YES;
}
方案2:也可以用系统的判断字符串的方法(应用举例)
// 获取当前版本号
NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
NSString *appVersion = [infoDic objectForKey:@"CFBundleShortVersionString"];
if ([appVersion compare:serverVersion options:NSNumericSearch] == NSOrderedAscending) {
UIAlertView *alert;
alert = [[UIAlertView alloc] initWithTitle:@"有新版本,是否升级!"
message:message
delegate: self
cancelButtonTitle:@"取消"
otherButtonTitles: @"升级", nil];
alert.tag = 20;
[alert show];
}else{
NSLog(@"没有新版本");
}
参数定义
options:(NSStringCompareOptions)
传入 NSStringCompareOptions 枚举的值
enum{
NSCaseInsensitiveSearch = 1,//不区分大小写比较
NSLiteralSearch = 2,//区分大小写比较
NSBackwardsSearch = 4,//从字符串末尾开始搜索
NSAnchoredSearch = 8,//搜索限制范围的字符串
NSNumbericSearch = 64//按照字符串里的数字为依据,算出顺序。例如 Foo2.txt < Foo7.txt < Foo25.txt
//以下定义高于 mac os 10.5 或者高于 iphone 2.0 可用
,
NSDiacriticInsensitiveSearch = 128,//忽略 "-" 符号的比较
NSWidthInsensitiveSearch = 256,//忽略字符串的长度,比较出结果
NSForcedOrderingSearch = 512//忽略不区分大小写比较的选项,并强制返回 NSOrderedAscending 或者 NSOrderedDescending
//以下定义高于 iphone 3.2 可用
,
NSRegularExpressionSearch = 1024//只能应用于 rangeOfString:..., stringByReplacingOccurrencesOfString:...和 replaceOccurrencesOfString:... 方法。使用通用兼容的比较方法,如果设置此项,可以去掉 NSCaseInsensitiveSearch 和 NSAnchoredSearch
}
range:(NSRange)
比较字符串的范围
结构变量:
location: 需要比较的字串起始位置(以0为起始)
length: 需要比较的字串长度
返回值:
typedef enum _NSComparisonResult {
NSOrderedAscending = -1, // < 升序
NSOrderedSame, // = 等于
NSOrderedDescending // > 降序
} NSComparisonResult;
比较App Store版本和本地是否一致
- (void)updateVersionAction {
//checkVersion
NSString *url = @"http://itunes.apple.com/cn/lookup?id=1576843916";
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/javascript", nil];
[manager POST:url parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
NSArray *results = responseObject[@"results"];
if (results.count > 0) {
NSString *serverVersion = responseObject[@"results"][0][@"version"];//获取版本号
// 获取当前版本号
NSString *appVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
//判断本地版本与App Store版本是否一致
if ([appVersion compare:serverVersion options:NSNumericSearch] == NSOrderedAscending) {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"有新版本,是否升级!" message:nil preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}];
[alertController addAction:cancelAction];
UIAlertAction *comfirmAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
// 无权限 引导去开启
if ([[UIApplication sharedApplication]canOpenURL:[NSURL URLWithString:url]]) {
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:url]];
}
}];
[alertController addAction:comfirmAction];
[self presentViewController:alertController animated:YES completion:nil];
}else{
[Toast toast:@"当前已是最新版本" toView:self.view];
}
}else{
[Toast toast:@"请去App Store查看是否有更新" toView:self.view];
}
} failure:^(NSURLSessionDataTask *task, NSError *error) {
[Toast toast:@"获取App Store最新版本失败" toView:self.view];
}];
}
网友评论