APP 版本更新提示
//版本检查
-
(void)checkAppVersion
{AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
NSString *kpath = @"http://itunes.apple.com/lookup?id=1123518551";
[manager GET:kpath parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nonnull responseObject) {NSDictionary *appInfo = (NSDictionary *)responseObject; NSArray *infoContent = [appInfo objectForKey:@"results"]; NSString *version = [[infoContent objectAtIndex:0] objectForKey:@"version"]; if ([self compareVersionWithServiceVersion:version]) { NSString * appVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]; UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"微店加更新提示", nil) message:[NSString stringWithFormat:@"%@\n最新版本:%@\n当前版本:%@",responseObject[@"results"][0][@"releaseNotes"], version,appVersion] preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *sureAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"抢鲜体验", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { NSURL *appUrl = [NSURL URLWithString:@"itms-apps://itunes.apple.com/app/id1123518551"]; if ([[UIApplication sharedApplication] canOpenURL:appUrl]) { if ([UIDevice currentDevice].systemVersion.floatValue < 10.0) { [[UIApplication sharedApplication] openURL:appUrl]; } else { [[UIApplication sharedApplication] openURL:appUrl options:@{UIApplicationOpenURLOptionUniversalLinksOnly:@NO} completionHandler:^(BOOL success) { NSLog(@"跳转设置%@", success ? @"成功" : @"失败"); }]; } } }]; [alertController addAction:sureAction]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"我偏不", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { }]; [alertController addAction:cancelAction]; [self.window.rootViewController presentViewController:alertController animated:YES completion:nil]; }
} failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
NSLog(@"查询iTunes应用信息错误:%@",error.description);
}];
} -
(BOOL)compareVersionWithServiceVersion:(NSString *)servierVersion
{
NSLog(@"商店的版本是 %@",servierVersion);
NSArray * serviceArr = [servierVersion componentsSeparatedByString:@"."];NSString * appVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
NSLog(@"当前的版本是 %@",appVersion);
NSArray * appArr = [appVersion componentsSeparatedByString:@"."];if (!serviceArr.count){
return NO;
}
else
{
NSInteger compare = 0;
if (serviceArr.count >= appArr.count)
{
compare = appArr.count;
}
else
{
compare = serviceArr.count;
}
for (int i = 0; i < compare; i++)
{
if ([serviceArr[i] integerValue] > [appArr[i] integerValue])
{
return YES;
}
else if ([serviceArr[i] integerValue] == [appArr[i] integerValue])
{
continue;
}
else
{
return NO;
}
}
return NO;
}
}