iOS APP compares version numbers and detects updates

 

1. Get the version number of the current APP

#pragma mark - 获取APP当前版本号
- (NSString *)getCurrentVersion {
	NSDictionary *infoDict   = [[NSBundle mainBundle] infoDictionary];
	NSString *currentVersion = [infoDict objectForKey:@"CFBundleShortVersionString"];
	NSLog(@"当前版本号:%@",currentVersion);
	return currentVersion;
}

 

2. Get iTunes App information

  1. Obtain APP information with BundleId: https://itunes.apple.com/lookup?bundleId=The Bundle ID of your APP
  2. Get APP information according to APPID: 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 is a dictionary {}

results = ()//This is an array with only one element, which is filled with app information, and each element is a dictionary. There are various keys inside.

Among them are trackName (name) trackViewUrl = (download address) version (displayable version number), etc.

 

3. Compare version numbers

 

/**
  比较两个版本号的大小

 @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. Complete call, pop up update prompt window

#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) {

	}];

}

 

Guess you like

Origin blog.csdn.net/zjpjay/article/details/89703980