1.获取本地版本信息
2.获取App Store版本信息
3.新旧版本对比
代码:
请求的基类中获取App Store版本信息:
+ (void)checkVersionSuccess:(HttpCheckSuccess)success failure:(HttpFailureStr)failure
{
NSString *vv = [NSString stringWithFormat:@"Version %@",[[NSBundle mainBundle]objectForInfoDictionaryKey:@"CFBundleShortVersionString"]];
NSString * _oldVersionValue = [vv substringFromIndex:8]; //当前Xcode中版本
//KAppId App Store上应用的ID
NSString *url_string = [NSString stringWithFormat:@"http://itunes.apple.com/lookup?id=%@", KAppId];
[[AppNetworking shareManager] GET:url_string parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSArray *appInfoItems = [responseObject objectForKey:@"results"];
if ([appInfoItems count] > 0) {
NSDictionary *appInfoDict = [appInfoItems objectAtIndex:0];
NSLog(@"appInfoDict: %@", appInfoDict);
//App Store线上版本
NSString *sysValue = [appInfoDict objectForKey:@"version"];
NSString *newSys = [sysValue stringByReplacingOccurrencesOfString:@"." withString:@""];
NSString *newOld = [_oldVersionValue stringByReplacingOccurrencesOfString:@"." withString:@""];
if (success) {
//新版本信息大于旧版本信息——>有更新。
success(sysValue,newOld.integerValue < newSys.integerValue);
}
}else
{
if (failure) {
failure(_oldVersionValue);
}
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"%@",error.localizedDescription);
if (error.code==3840) {
if (failure) {
failure(@"网络开小差啦 请稍后再试");
}
return ;
}
if (failure) {
failure(_oldVersionValue);
}
}];
}
AppDelegate中调用上述方法进行提示功能。
- (void)applicationWillEnterForeground:(UIApplication *)application
{
if (![[NSUserDefaults standardUserDefaults] boolForKey:@"updateMessage"]) return;
[AppNetworking checkVersionSuccess:^(NSString *version, BOOL update) {
if (update) {
NSString *msg = [NSString stringWithFormat:@"为不影响正常使用,请到App Store更新模块下载最新版本%@",version];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"版本更新提醒" message:msg delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
[alert show];
}
} failure:^(NSString *errorMessage) {
}];
}
//提示更新
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"updateMessage"];
[[NSUserDefaults standardUserDefaults] synchronize];
if (buttonIndex == 1) return;
NSString *appstoreUrlString = [NSString stringWithFormat:@"itms-apps://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=%@",KAppId];
NSURL * url = [NSURL URLWithString:appstoreUrlString];
if ([[UIApplication sharedApplication] canOpenURL:url])
{
[[UIApplication sharedApplication] openURL:url];
}
else
{
NSLog(@"url can not open");
}
}