前語
最近一個對我來說相對的難點需求:從接口下載版本不一樣的zip包,保存到本地,解壓縮,然后讀取本地文件夾內(nèi)的html,這期間遇到了很多的問題,總結(jié)如下:
模塊
- 準備工作
- 遇到的問題
- 根據(jù)接口返回的地址下載
- 把zip包保存本地,解壓縮保存文件夾
- 讀取本地文件夾內(nèi)的html,展示webView
1.準備工作
解壓縮的需要第三方的解壓縮庫,換過幾個解壓庫,都是c/c++寫的,如:SSZipArchive,ZipArchive等。
*先把SSZipArchive拖進工程
*然后加入系統(tǒng)庫 libz.dylib
*在pch引入:
#ifdef __OBJC__
#import "ZipArchive.h"
#endif
2.遇到的問題
*解壓縮的庫里面是c/c++寫的,所以工程需要修改配置,因為oc與c/c++ 要混編 *
如果直接拖進 SSZipArchive ,編譯會報錯Unknown type name 'NSString',錯誤提示如下圖:
Unknown type name 'NSString'.png
一般情況下出現(xiàn)“Unknown type name”是頭文件互相引用出現(xiàn)的,這里可以排除,由于源碼使用是c\c++與oc混編,因為解壓縮庫里面是c/c++,如圖:
解壓縮庫.jpg
嘗試了幾種方案:
解決方案一:選擇所有.c文件,將屬性的 identity and type 改為Objective-C Source
sources.jpg
解決方案二:選擇所有.c文件,將.c修改為.m
解決方案三:由于修改所有文件的編譯類型,所有可能會導致其他包括c、c++代碼的提示錯誤,修改如下,但是也會報錯,最好是保留下面如圖的配置,按照一二種方案解決。
compile sources As.png
Apple LLVM 8.0 - Language -C++.png
到此,整個解壓縮的庫算是編譯通過了,但是我這邊的工程又遇到很奇葩的問題,跟環(huán)信的sdk里面的好多方法名沖突了,我也表示很無語,太辣眼睛了,如圖:
error.png
因為環(huán)信所有的方法都是靜態(tài)庫點a里面的東西,我們這種免費用人家?guī)斓娜耍薷牟涣巳思异o態(tài)庫的東西,只能自己默默修改SSZipArchive里面的方法名,我的天呢35個方法名,加班加點的,最后終于搞定。至此我也很無語,試了好幾個第三方的解壓縮軟件,都是c/c++寫的,都是跟環(huán)信的sdk的方法名沖突,只能自己一個一個修改。
3.根據(jù)接口返回的地址下載,解壓保存到文件夾
#因為我們是根據(jù)接口版本號判斷給的zip是不是最新的再保存到本地,然后讀取本地的解壓縮之后的文件夾。所以在此之前我先寫了一個本地的plist來存取zip的版本號。本地plist的寫法如下:
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *filePatch = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]stringByAppendingPathComponent:@"OrderHtmlList.plist"];
BOOL Exists = [fileManager fileExistsAtPath:filePatch];
if(!Exists){
NSString *plistPath = [[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:@"OrderHtmlList.plist"];
NSError *error;
BOOL success = [fileManager copyItemAtPath:plistPath toPath:filePatch error:&error];
if(!success){
NSAssert1(0, @"錯誤寫入文件:'%@'.", [error localizedDescription]);
}
}
#再根據(jù)版本號判斷,是否下載。第一次先保存一個空的版本號為空,直接走網(wǎng)絡(luò)下載,然后保存當前下載完之后的版本號,再次運行app的時候,判斷保存的版本號,跟接口給的版本號是否一致,一致的話,就不用下載,不一致,就下載新的。如下判斷:
NSMutableDictionary *usersDic = [[NSMutableDictionary alloc] initWithContentsOfFile:filePatch];
if ([LKTools isBlankString:[usersDic objectForKey:@"htmlVersion"]]) {
//保存新html的版本號
NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]stringByAppendingPathComponent:@"OrderHtmlList.plist"];
NSMutableDictionary *applist = [[[NSMutableDictionary alloc]initWithContentsOfFile:path]mutableCopy];
NSString *name = [applist objectForKey:@"htmlVersion"];
name = [dic objectForKey:@"hnum"];
[applist setObject:name forKey:@"htmlVersion"];
[applist writeToFile:path atomically:YES];
NSString *htmlFilePath = [NSString stringWithFormat:@"%@",SERVER_HOST([dic objectForKey:@"hurl"])];
//下載解壓縮
[self rquestZipArchivePath:htmlFilePath andHtmlVersion:[dic objectForKey:@"htmlVersion"]];
}
else{
if ([[usersDic objectForKey:@"htmlVersion"] isEqualToString:[dic objectForKey:@"hnum"]]) {
NSLog(@"不下載不解壓");
}
else{
//保存新html的版本號
NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]stringByAppendingPathComponent:@"OrderHtmlList.plist"];
NSMutableDictionary *applist = [[[NSMutableDictionary alloc]initWithContentsOfFile:path]mutableCopy];
NSString *name = [applist objectForKey:@"htmlVersion"];
name = [dic objectForKey:@"hnum"];
[applist setObject:name forKey:@"htmlVersion"];
[applist writeToFile:path atomically:YES];
NSString *htmlFilePath = [NSString stringWithFormat:@"%@",SERVER_HOST([dic objectForKey:@"hurl"])];
//下載解壓縮
[self rquestZipArchivePath:htmlFilePath andHtmlVersion:[dic objectForKey:@"htmlVersion"]];
}
}
#pragma mark 請求zip地址
-(void)rquestZipArchivePath:(NSString *)pathUrl andHtmlVersion:(NSString *)version{
//遠程地址
NSURL *URL = [NSURL URLWithString:pathUrl];
//默認配置
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
//請求
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
NSURLSessionDownloadTask * downloadTask= [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
//- block的返回值, 要求返回一個URL, 返回的這個URL就是文件的位置的路徑
NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
//再次之前先刪除本地文件夾里面相同的文件夾
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *contents = [fileManager contentsOfDirectoryAtPath:cachesPath error:NULL];
NSEnumerator *e = [contents objectEnumerator];
NSString *filename;
NSString *extension = @"zip";
while ((filename = [e nextObject])) {
if ([[filename pathExtension] isEqualToString:extension]) {
[fileManager removeItemAtPath:[cachesPath stringByAppendingPathComponent:filename] error:NULL];
}
}
NSString *path = [cachesPath stringByAppendingPathComponent:response.suggestedFilename];
return [NSURL fileURLWithPath:path];
} completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
//設(shè)置下載完成操作
// filePath就是你下載文件的位置,你可以解壓,也可以直接拿來使用
NSString *htmlFilePath = [filePath path];// 將NSURL轉(zhuǎn)成NSString
NSArray *documentArray = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *path = [[documentArray lastObject] stringByAppendingPathComponent:@"Preferences"];
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager removeItemAtPath:[NSString stringWithFormat:@"%@/html",path] error:nil];
[self releaseZipFilesWithUnzipFileAtPath:htmlFilePath Destination:path];
}];
[downloadTask resume];
}
#pragma mark 解壓
- (void)releaseZipFilesWithUnzipFileAtPath:(NSString *)zipPath Destination:(NSString *)unzipPath{
// NSLog(@"%@,%@",zipPath,unzipPath);
NSError *error;
if ([SSZipArchive unzipFileAtPath:zipPath toDestination:unzipPath overwrite:YES password:nil error:&error delegate:self]) {
NSLog(@"success");
}
else{
NSLog(@"%@",error);
}
}
#pragma mark - SSZipArchiveDelegate
- (void)zipArchiveWillUnzipArchiveAtPath:(NSString *)path zipInfo:(unz_global_info)zipInfo {
NSLog(@"將要解壓。");
}
- (void)zipArchiveDidUnzipArchiveAtPath:(NSString *)path zipInfo:(unz_global_info)zipInfo unzippedPath:(NSString *)unzippedPat uniqueId:(NSString *)uniqueId {
NSLog(@"解壓完成!");
}
4.讀取本地保存的html
先看一下保存解壓縮之后的html的文件夾,綠色箭頭指示的是我解壓完之后的整個文件夾,紅色的是我要進去讀取的html所在的文件夾,如圖:
23F2CBE4-186A-473A-8E34-CEE674CD823D.png
#讀取方法如下:
NSArray *documentArray = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *path = [[documentArray lastObject] stringByAppendingPathComponent:@"Preferences"];
NSURL *url=[NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/html/%@",path,self.orderHtmlStr]];
NSString *urlStr = [url absoluteString];
urlStr = [urlStr stringByReplacingOccurrencesOfString:@"file://" withString:@""];
NSURL * URL = [NSURL URLWithString:urlStr];
NSURLRequest *request=[NSURLRequest requestWithURL:URL];
[self.orderWebView loadRequest:request];