iOS 企业版应用一键更新
如果后台没有获取版本号的接口,自己就直接从下载企业版应用指向的 plist
文件中获取吧,如果有,就不用这么麻烦还要去下载整个 plist
文件了,直接根据拿到的版本好与本地对比即可。
#pragma mark -
#pragma mark 检查更新
- (void)checkForUpdating {
// 先清空之前下载的缓存文件
NSString *tmpDirectory = NSTemporaryDirectory();
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *cacheFiles = [fileManager contentsOfDirectoryAtPath:tmpDirectory error:&error];
for (NSString *file in cacheFiles) {
error = nil;
[fileManager removeItemAtPath:[tmpDirectory stringByAppendingPathComponent:file] error:&error];
}
// 从服务器下载plist文件
NSURLRequest *downloadRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:CheckUpdatingPlistUrl]];
NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *urlSession = [NSURLSession sessionWithConfiguration:sessionConfig delegate:nil delegateQueue:nil];
NSURLSessionDownloadTask *task = [urlSession downloadTaskWithRequest:downloadRequest completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (error) {
WJLog(@"获取下载服务器版本失败:\n%@", error.localizedDescription);
}
else {
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfURL:location];
// 服务器版本号
NSString *serverVersion = dict[@"items"][0][@"metadata"][@"bundle-version"];
// 本地版本号
NSString *localVersion = [[NSBundle mainBundle] infoDictionary][@"CFBundleShortVersionString"];
// 对比版本号,升序
if ([localVersion compare:serverVersion options:NSNumericSearch] == NSOrderedAscending) {
dispatch_async(dispatch_get_main_queue(), ^{
// 先提示安装
FCAlertView *alert = [[FCAlertView alloc] init];
[alert showAlertWithTitle:@"提示" withSubtitle:@"新版本已发布,点击确定开始更新" withCustomImage:nil withDoneButtonTitle:nil andButtons:nil];
alert.hideDoneButton = YES;
alert.bounceAnimations = YES;
alert.colorScheme = alert.flatOrange;
[alert makeAlertTypeCaution];
alert.subTitleColor = [UIColor redColor];
[alert addButton:@"确定" withActionBlock:^{
// 模拟器不支持
if (TARGET_IPHONE_SIMULATOR) { WJLog(@"模拟器不支持该操作"); return; }
// 真机执行安装
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"itms-services://?action=download-manifest&url=%@", CheckUpdatingPlistUrl]];
[[UIApplication sharedApplication] openURL:url];
// 动画退出App
UIWindow *window = [UIApplication sharedApplication].keyWindow;
[UIView animateWithDuration:.5f animations:^{
window.alpha = 0;
CGFloat width = window.bounds.size.width;
CGFloat height = window.bounds.size.height;
window.frame = CGRectMake(width * 0.5, height * 0.5, 0, 0);
} completion:^(BOOL finished) {
exit(0);
}];
}];
[alert addButton:@"下次再说" withActionBlock:nil];
});
}
}
}];
[task resume];
}
那个 FCAlertView
没有可以用原生的,比如:
// 对比版本号,升序
if ([localVersion compare:serverVersion options:NSNumericSearch] == NSOrderedAscending) {
__weak typeof(self) weakSelf = self;
dispatch_async(dispatch_get_main_queue(), ^{
// 先提示安装
UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:@"提示" message:@"新版本已发布,点击确定开始更新" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *confirmAct = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
// 模拟器不支持
if (TARGET_IPHONE_SIMULATOR) { WJLog(@"模拟器不支持该操作"); return; }
// 真机执行安装
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"itms-services://?action=download-manifest&url=%@", CheckUpdatingPlistUrl]];
[[UIApplication sharedApplication] openURL:url];
// 动画退出App
UIWindow *window = [UIApplication sharedApplication].keyWindow;
[UIView animateWithDuration:.5f animations:^{
window.alpha = 0;
CGFloat width = window.bounds.size.width;
CGFloat height = window.bounds.size.height;
window.frame = CGRectMake(width * 0.5, height * 0.5, 0, 0);
} completion:^(BOOL finished) {
exit(0);
}];
}];
// 取消
UIAlertAction *cancelAct = [UIAlertAction actionWithTitle:@"下次再说" style:UIAlertActionStyleDefault handler:nil];
[alertVc addAction:confirmAct];
[alertVc addAction:cancelAct];
[weakSelf.window.rootViewController presentViewController:alertVc animated:YES completion:nil];
});
}