AppVersion.h
跳转App Store链接:https://apps.apple.com/cn/app/id这里填写AppleID
检测更新:http://itunes.apple.com/cn/lookup?id=这里填写AppleID
/** 本地版本号 */
+ (NSString *)locationVersion;
/** 检测版本并回调 */
+ (void)checkNewVersionNotificationBlock:(void(^)(BOOL success, NSDictionary * _Nullable result))block;
/** 提示更新并回调 */
+ (void)showUpdateTips:(NSDictionary *)dictionary completion:(void(^__nullable)(NSInteger action))block;
AppVersion.m
- version:版本号
- releaseNotes:更新日志
- trackViewUrl:iTunes链接
@implementation AppVersion
// 本地版本号
+ (NSString *)locationVersion
{
NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
NSString *nowVersion = [infoDictionary valueForKey:@"CFBundleShortVersionString"];
return nowVersion;
}
// Appstore 版本号
+ (void)getNewVersion:(void(^)(BOOL success, NSDictionary *result))block
{
NSString *urlstr = [NSString stringWithFormat:@"http://itunes.apple.com/cn/lookup?id=%@", kAppleID];
NSURL *url = [NSURL URLWithString:urlstr];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setTimeoutInterval:10];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (data) {
if (!error) {
NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
NSArray *results = [dictionary valueForKey:@"results"];
if (results.count > 0) {
NSDictionary *dic = results[0];
dispatch_async(dispatch_get_main_queue(), ^{
if (block) {
block(YES, dic);
}
});
return ;
} else {
// 已是最新版本
dispatch_async(dispatch_get_main_queue(), ^{
if (block) {
block(YES, nil);
}
});
return;
}
}
}
// 请求失败
dispatch_async(dispatch_get_main_queue(), ^{
if (block) {
block(NO, nil);
}
});
}];
[task resume];
}
// 比较版本号
+ (BOOL)shouldUpdateNowVersion:(NSString *)nowVersion newVersion:(NSString *)newVersion
{
NSComparisonResult result = [newVersion compare:nowVersion options:NSNumericSearch];
switch (result) {
case NSOrderedDescending:
return YES;
default:
return NO;
}
}
// 检测版本并回调
+ (void)checkNewVersionNotificationBlock:(void(^)(BOOL success, NSDictionary *_Nullable result))block
{
[self getNewVersion:^(BOOL success, NSDictionary *result) {
if (success) {
if (result) {
NSString *newVersion = [result valueForKey:@"version"];
NSString *nowVersion = [self locationVersion];
BOOL shouldUpdate = [self shouldUpdateNowVersion:nowVersion newVersion:newVersion];
if (shouldUpdate) {
// 版本不同则有更新
dispatch_main_async_safe(^{
if (block) {
block(success, result);
}
});
return ;
}
}
}
dispatch_main_async_safe(^{
if (block) {
block(success, nil);
}
});
}];
}
// 弹出提示信息
+ (void)showUpdateTips:(NSDictionary *)dictionary
{
[self showUpdateTips:dictionary completion:nil];
}
// 弹出提示信息并回调
+ (void)showUpdateTips:(NSDictionary *)dictionary completion:(void(^__nullable)(NSInteger action))block
{
NSString *version = [dictionary objectForKey:@"version"];
NSString *releaseNotes = [dictionary valueForKey:@"releaseNotes"];
if (releaseNotes.length == 0) {
releaseNotes = @" ";
}
releaseNotes = [releaseNotes stringByReplacingOccurrencesOfString:@"\n" withString:@""];
NSString *trackViewUrl = [dictionary valueForKey:@"trackViewUrl"];
NSString *title = [NSString stringWithFormat:@"版本更新(v%@)", version];
NSString *message = [NSString stringWithFormat:@"%@\n现在更新?", releaseNotes];
//有新版本
UIAlertController *alertCtr = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *buttonAct = [UIAlertAction actionWithTitle:@"更新" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSString *urlStr = trackViewUrl;
NSURL *url = [NSURL URLWithString:urlStr];
[[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
if (block) {
block(1);
}
}];
UIAlertAction *cancelAct = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
if (block) {
block(0);
}
}];
[alertCtr addAction:buttonAct];
[alertCtr addAction:cancelAct];
UIViewController *vc = [UIViewController topViewController];
[vc presentViewController:alertCtr animated:YES completion:nil];
}
topViewController方法
// 获得顶层的viewController
+ (UIViewController *)topViewController {
UIViewController *resultVC;
resultVC = [self _topViewController:[[UIApplication sharedApplication].keyWindow rootViewController]];
while (resultVC.presentedViewController) {
resultVC = [self _topViewController:resultVC.presentedViewController];
}
return resultVC;
}
+ (UIViewController *)_topViewController:(UIViewController *)vc {
if ([vc isKindOfClass:[UINavigationController class]]) {
return [self _topViewController:[(UINavigationController *)vc topViewController]];
} else if ([vc isKindOfClass:[UITabBarController class]]) {
return [self _topViewController:[(UITabBarController *)vc selectedViewController]];
} else {
return vc;
}
return nil;
}
关于版本号的比较
NSComparisonResult result = [newVersion compare:nowVersion options:NSNumericSearch];
result值有三个
NSOrderedAscending 从低到高
NSOrderedSame 相等
NSOrderedDescending 从高到低
NSNumericSearch 是按照ascii码来进行逐位比较大小
Numbers within strings are compared using numeric value, that is, Name2.txt < Name7.txt < Name25.txt.
例:本地版本号(手机当前安装的app版本号) 1.1
AppStore版本号 1.1.2
新版本和旧版本相比较
NSComparisonResult result = [newVersion compare:nowVersion options:NSNumericSearch];
1=1,比较下一位"."=".",1=1,"."=".",nil<2。最好一位本地为空,AppStore为2,所以2大于空。
result 结果就是NSOrderedDescending,新版本大于旧版本,有更新。
我以前的做法是把字符串分割成数组,不足三位的后面补个0,然后一个一个比较。虽然结果相同但是相对来说比较复杂,多写几行代码。用这个方法一行代码得到结果,很是方便。