apple 现在虽然不然热更新了,但是好多时候,我们会发现为了解决一点小问题就需要重新上线的尴尬,为了解决这个问题,分享服务器更新的思路,希望对大家有所帮助
首先我们应该创建版本更新管理类,主要的方法如下所示
1. 判断是否是第一次启动app,如果是第一次启动,将资源文件copy到沙盒
// 如果沙箱指定目录已存在,先删除他
if([fileManager fileExistsAtPath:[[self class] resourceRootPath]]) {
if(![fileManager removeItemAtPath:[[self class] resourceRootPath] error:&error]) {
DLog(@"remove resourceFolder error: %@", error.localizedDescription);
return NO;
}
}
// 拷贝资源文件到沙箱指定目录
NSString *bundlePath = [[NSBundle mainBundle] pathForResource:bundleName ofType:nil];
if(![fileManager copyItemAtPath:bundlePath toPath:[[self class] resourceRootPath] error:&error]) {
DLog(@"copy resource files error: %@", error.localizedDescription);
return NO;
if([fileManager fileExistsAtPath:[[self class] resourceRootPath]]) {
if(![fileManager removeItemAtPath:[[self class] resourceRootPath] error:&error]) {
DLog(@"remove resourceFolder error: %@", error.localizedDescription);
return NO;
}
}
// 拷贝资源文件到沙箱指定目录
NSString *bundlePath = [[NSBundle mainBundle] pathForResource:bundleName ofType:nil];
if(![fileManager copyItemAtPath:bundlePath toPath:[[self class] resourceRootPath] error:&error]) {
DLog(@"copy resource files error: %@", error.localizedDescription);
return NO;
}
2. 初始化文件索引,将沙盒中的资源文件的索引添加到sqlite 数据库
3. 检查更新,将数据库中的版本号和服务器上的版本号进行对比,如果服务器上的版本号大,在沙盒中创建新版本的资源目录,并且开始下载服务器文件,更新数据库
// 开始下载,文件名会自动去取url的文件名
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:versionRecord.downurl] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60];
AFDownloadRequestOperation *operation = [[AFDownloadRequestOperation alloc] initWithRequest:request targetPath:targetFullPath shouldResume:YES];
operation.shouldOverwrite = YES;
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation * _Nonnull operation, id _Nonnull responseObject) {
// 下载成功才更新索引数据库
[[self class] updateIndexDB:versionRecord];
DLog(@"update file \"%@/%@\" from version %ld to version %ld",versionRecord.filepath, versionRecord.filename, version, versionRecord.versionno);
} failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) {
DLog(@"download resource(%@) error: %@", operation.request.URL.absoluteString, error.localizedDescription);
}];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:versionRecord.downurl] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60];
AFDownloadRequestOperation *operation = [[AFDownloadRequestOperation alloc] initWithRequest:request targetPath:targetFullPath shouldResume:YES];
operation.shouldOverwrite = YES;
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation * _Nonnull operation, id _Nonnull responseObject) {
// 下载成功才更新索引数据库
[[self class] updateIndexDB:versionRecord];
DLog(@"update file \"%@/%@\" from version %ld to version %ld",versionRecord.filepath, versionRecord.filename, version, versionRecord.versionno);
} failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) {
DLog(@"download resource(%@) error: %@", operation.request.URL.absoluteString, error.localizedDescription);
}];
[[NSOperationQueue
mainQueue]
addOperation:operation];