采用 "https://itunes.apple.com/cn/lookup?id=" + 版本号获取(如果是针对全球上线的用https://itunes.apple.com/lookup?id=) app 信息与本地版本号比较确定是否提示更新.实现代码如下:
/**
判断 APP 是否有更新
@param appleid app 在 appstore 的 id 号
*/
- (void)ay_checkAPPVersionWithAppleID:(NSString*)appleid{
NSError *error ;
NSData *appInfoData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://itunes.apple.com/cn/lookup?id=%@",appleid]] options:NSDataReadingUncached error:&error];
if (!error) {
NSDictionary *appInfoDic = [NSJSONSerialization JSONObjectWithData:appInfoData options:NSJSONReadingMutableContainers error:nil];
NSString *appInfoVersion = appInfoDic[@"results"][0][@"version"];// APPStore 版本号
NSString *currentVersion = [NSBundle mainBundle].infoDictionary[@"CFBundleShortVersionString"];
NSString *ceachVersion = [[NSUserDefaults standardUserDefaults] stringForKey:@"CFBundleShortVersionString"];//沙盒缓存版本号
if ([self ay_getVersionWithStr:appInfoVersion] > [self ay_getVersionWithStr:currentVersion] && ![ceachVersion isEqualToString:appInfoVersion]) {
//将 appstore 中的版本号同步到本地
[[NSUserDefaults standardUserDefaults] setValue:appInfoVersion forKey:@"CFBundleShortVersionString"];
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"更新提示信息" message:nil preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
if (iOS10orlater) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"app 下载地址"] options:[NSDictionary dictionary] completionHandler:nil];
} else {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"app 下载地址"]];
}
}];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
[alertVC addAction:okAction];
[alertVC addAction:cancelAction];
[self.window.rootViewController presentViewController:alertVC animated:YES completion:nil];
}
}
}