iOS APP 比较版本号,检测更新
程序员文章站
2022-05-19 22:07:53
...
1.获取当前APP的版本号
#pragma mark - 获取APP当前版本号
- (NSString *)getCurrentVersion {
NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary];
NSString *currentVersion = [infoDict objectForKey:@"CFBundleShortVersionString"];
NSLog(@"当前版本号:%@",currentVersion);
return currentVersion;
}
2.获取iTunes App信息
- 以BundleId获取APP信息:https://itunes.apple.com/lookup?bundleId=你APP的Bundle ID
- 根据APPID 获取APP信息:http://itunes.apple.com/cn/lookup?id=你的APPID
NSString *APP_URL = @"http://itunes.apple.com/cn/lookup?id=1457293407"
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager POST:APP_URL parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSArray *array = [responseObject objectForKey:@"results"];
NSDictionary *dic = [array firstObject];
NSString *versionStr = [dic objectForKey:@"version"];// 版本号
NSString *trackViewUrl = [dic objectForKey:@"trackViewUrl"];// App Store网址
NSString *releaseNotes = [dic objectForKey:@"releaseNotes"];//更新日志信息
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
}];
responseObject是个字典{}
results =()//这是个只有一个元素的数组,里面都是app信息,每个一个元素就是一个字典。里面有各种key。
其中有 trackName (名称)trackViewUrl = (下载地址)version (可显示的版本号)等等
3.比较版本号
/**
比较两个版本号的大小
@param version APP Store 版本号
@return 版本号相等,返回NO;大于 返回YES
*/
- (BOOL)compareVesionWithServerVersion:(NSString *)version {
NSArray *versionArray = [version componentsSeparatedByString:@"."];//拿到iTunes获取App的版本
NSArray *currentVesionArray = [[self getCurrentVersion] componentsSeparatedByString:@"."];//当前版本
NSInteger a = (versionArray.count> currentVesionArray.count)?currentVesionArray.count : versionArray.count;
BOOL haveNew = NO;
for (int i = 0; i < a; i++) {
NSInteger a = [[versionArray objectAtIndex:i] integerValue];
NSInteger b = [[currentVesionArray objectAtIndex:i] integerValue];
if (a > b) {
haveNew = YES;
}else{
haveNew = NO;
}
}
if (haveNew) {
NSLog(@"APP store 版本号大于当前版本号:有新版本更新");
}else{
NSLog(@"APP store 版本号小于等于当前版本号:没有新版本");
}
return haveNew;
}
4.完整调用,弹出更新提示窗
#pragma mark -
//检查更新
- (void)checkUpVersionUpdate {
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager POST:APP_URL parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSArray *array = [responseObject objectForKey:@"results"];
NSDictionary *dic = [array firstObject];
NSString *versionStr = [dic objectForKey:@"version"];// 版本号
NSString *trackViewUrl = [dic objectForKey:@"trackViewUrl"];// App Store网址
NSString *releaseNotes = [dic objectForKey:@"releaseNotes"];//更新日志信息
if (versionStr && ![versionStr isEqualToString:@""]) {//版本存在并且不为空
if ([self compareVesionWithServerVersion:versionStr]) {//比较版本号
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:[NSString stringWithFormat:@"发现新版本:%@\n为了不影响使用请更新到最新版本",versionStr] message:releaseNotes preferredStyle:UIAlertControllerStyleAlert];
// 设置标题和内容的对齐方式
UIView *subView1 = alertVC.view.subviews[0];
UIView *subView2 = subView1.subviews[0];
UIView *subView3 = subView2.subviews[0];
UIView *subView4 = subView3.subviews[0];
UIView *subView5 = subView4.subviews[0];
//分别拿到title 和 message 可以分别设置他们的对齐属性
// 1. 第一种设置方法
// UILabel *title = subView5.subviews[1];
// UILabel *message = subView5.subviews[2];
// title.textAlignment = NSTextAlignmentLeft;
// message.textAlignment = NSTextAlignmentLeft;
// 1. 第二种设置方法
NSInteger i = 0;
for( UIView * view in subView5.subviews ){
if( [view isKindOfClass:[UILabel class]] ){
i++;
if (i==2) {
UILabel* label = (UILabel*) view;
label.textAlignment = NSTextAlignmentLeft;
}
}
}
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
//NSLog(@"点击了取消");
}];
UIAlertAction *OKAction = [UIAlertAction actionWithTitle:@"去更新" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//NSLog(@"点击了知道了");
NSURL * url = [NSURL URLWithString:trackViewUrl];//itunesURL = trackViewUrl的内容
[[UIApplication sharedApplication] openURL:url];
}];
[alertVC addAction:cancelAction];
[alertVC addAction:OKAction];
[self presentViewController:alertVC animated:YES completion:nil];
}
else{
//当前已是最新版本
}
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
}];
}
下一篇: MySQL中找不到.myd文件的问题