1.現在のAPPのバージョン番号を取得します
#pragma mark - 获取APP当前版本号
- (NSString *)getCurrentVersion {
NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary];
NSString *currentVersion = [infoDict objectForKey:@"CFBundleShortVersionString"];
NSLog(@"当前版本号:%@",currentVersion);
return currentVersion;
}
2.iTunesアプリ情報を取得します
- BundleIdを使用してAPP情報を取得します:https://itunes.apple.com/lookup?bundleId = APPのバンドルID
- APPIDに従ってAPP情報を取得します:http: //itunes.apple.com/cn/lookup?id = your 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 =()//これは、アプリ情報が入力された要素が1つだけの配列であり、各要素は辞書です。中にはいろいろな鍵があります。
その中には、trackName(名前)trackViewUrl =(ダウンロードアドレス)バージョン(表示可能なバージョン番号)などがあります。
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) {
}];
}