- (BOOL)compareOriginVersion:(NSString *)version withNewVersion:(NSString *)newVersion{
NSArray *versionArray0 = [version componentsSeparatedByString:@"."];
NSArray *versionArray1 = [newVersion componentsSeparatedByString:@"."];
NSInteger count = versionArray0.count <= versionArray1.count ? versionArray0.count :versionArray1.count;
for (int i = 0; i < count; i++) {
NSString *v0 = [versionArray0 objectAtIndex:i];
NSString *v1 = [versionArray1 objectAtIndex:i];
if (v0.length > v1.length) {
return NO;
}else if(v0.length < v1.length){
return YES;
}
if ([v0 compare:v1] == NSOrderedAscending) {
return YES;
}else if ([v0 compare:v1] == NSOrderedDescending){
return NO;
}
}
if (versionArray0.count > versionArray1.count) {
return NO;
}else{
return YES;
}
}
利用iTunes接口检查App版本更新
/// 检查版本更新
- (void)checkVersion {
NSURL * url = [NSURL URLWithString:@"http://itunes.apple.com/lookup?id=414478124"];
[[[NSURLSession sharedSession] dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSDictionary * dataDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
NSArray *results = dataDic[@"results"];
if (results && results.count > 0) {
NSDictionary *response = results.firstObject;
NSString *currentVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"]; // 软件的当前版本
NSString *lastestVersion = response[@"version"]; // AppStore 上软件的最新版本
if (currentVersion && lastestVersion && ![self isLastestVersion:currentVersion compare:lastestVersion]) {
NSString * releaseNotes = response[@"releaseNotes"]; // 新版本更新内容
NSString * alertContent = [NSString stringWithFormat:@"%@\n\n是否前往 AppStore 更新版本?",releaseNotes];
// 给出提示是否前往 AppStore 更新
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"检测到有版本更新" message:alertContent preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"前往" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
NSString *trackViewUrl = response[@"trackViewUrl"]; // AppStore 上软件的地址
if (trackViewUrl) {
NSURL *appStoreURL = [NSURL URLWithString:trackViewUrl];
if ([[UIApplication sharedApplication] canOpenURL:appStoreURL]) {
[[UIApplication sharedApplication] openURL:appStoreURL];
}
}
}]];
[alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
[[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alert animated:YES completion:nil];
}
}
}] resume];
}
/// 判断是否最新版本号(大于或等于为最新)
- (BOOL)isLastestVersion:(NSString *)currentVersion compare:(NSString *)lastestVersion {
if (currentVersion && lastestVersion) {
// 拆分成数组
NSMutableArray *currentItems = [[currentVersion componentsSeparatedByString:@"."] mutableCopy];
NSMutableArray *lastestItems = [[lastestVersion componentsSeparatedByString:@"."] mutableCopy];
// 如果数量不一样补0
NSInteger currentCount = currentItems.count;
NSInteger lastestCount = lastestItems.count;
if (currentCount != lastestCount) {
NSInteger count = labs(currentCount - lastestCount); // 取绝对值
for (int i = 0; i < count; ++i) {
if (currentCount > lastestCount) {
[lastestItems addObject:@"0"];
} else {
[currentItems addObject:@"0"];
}
}
}
// 依次比较
BOOL isLastest = YES;
for (int i = 0; i < currentItems.count; ++i) {
NSString *currentItem = currentItems[i];
NSString *lastestItem = lastestItems[i];
if (currentItem.integerValue != lastestItem.integerValue) {
isLastest = currentItem.integerValue > lastestItem.integerValue;
break;
}
}
return isLastest;
}
return NO;
}