iOS文件下载解压,WKWebView加载沙盒html

一、文件下载:

/**
 下载新的版本
 */
- (void)downLoadNewVersion {
    WS(weakSelf);
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    NSString * urlStr = [NSString stringWithFormat:@"http://xxxx/dist/app/xx.zip"];
    // 下载地址
    NSURL *url = [NSURL URLWithString:urlStr];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    // 获取Document
    NSString *documentDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    // 文件夹完整路径
    NSString *docPath = [documentDirectory stringByAppendingPathComponent:@"Resource"];
    // 下载存放路径
    NSString *savePath = [docPath stringByAppendingPathComponent:url.lastPathComponent];
    // 解压路径
    NSString *unzipPath = docPath;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL isDir = YES;
    // fileExistsAtPath 判断一个文件或目录是否有效,isDirectory判断是否一个目录
    BOOL existed = [fileManager fileExistsAtPath:docPath isDirectory:&isDir];
    NSError *error = nil;
    // 如果文件夹存在
    if (existed) {
        // 移除原有文件
        BOOL isRemoveSuccess = [fileManager removeItemAtPath:docPath error:&error];
        if (!isRemoveSuccess) {
            NSLog(@"remove Resource failed with error:%@", error);
        }
    }
    BOOL isCreateSuccess = [fileManager createDirectoryAtPath:docPath withIntermediateDirectories:YES attributes:nil error:&error];
    if (isCreateSuccess) {
       // 开始请求下载
       NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
           NSLog(@"下载进度:%.0f%", downloadProgress.fractionCompleted * 100);
       } destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
           // 设定下载到的位置
           return [NSURL fileURLWithPath:savePath];
                   
       } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
            NSLog(@"--filePath:%@", filePath);
            NSLog(@"--error:%@", error);
            NSLog(@"--response:%@", response);
           if (!error) {
               [weakSelf decompressionFileToPath:unzipPath filePath:savePath];
           }

       }];
    [downloadTask resume];
    } else {
        NSLog(@"Create Resource failed with error:%@", error);
    }
}

二、文件解压:

使用工具ZipArchive
下载地址: ZipArchive

错误信息:dyld: Library not loaded: @rpath/ZipArchive.framework/
解决:

  • CC629D5C-F2B2-45D1-8FF4-50755CA35323.png
/**
 解压到指定文件目录
 */
- (void)decompressionFileToPath:(NSString *)targetDir filePath:(NSString *)filePath {
    [SSZipArchive unzipFileAtPath:filePath toDestination:targetDir progressHandler:^(NSString * _Nonnull entry, unz_file_info zipInfo, long entryNumber, long total) {
        NSLog(@"entryNumber:%ld", entryNumber);
        NSLog(@"total:%ld", total);
        NSLog(@"entry:%@", entry);
        
    } completionHandler:^(NSString * _Nonnull path, BOOL succeeded, NSError * _Nullable error) {
        if (succeeded) {
            // 移除原有文件
            NSError *err;
            NSFileManager *fileManager = [NSFileManager defaultManager];
            BOOL isRemoveSuccess = [fileManager removeItemAtPath:filePath error:&err];
            if (!isRemoveSuccess) {
                NSLog(@"remove tc.zip failed with error:%@", error);
            }
        } else {
            NSLog(@"unzip failed error:%@", error);
        }
    }];
}

三、WKWebView加载沙盒html

   NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Resource"];
   NSString *tcPath = [path stringByAppendingPathComponent:@"xx/index.html"];
   NSURL *url = [NSURL fileURLWithPath:tcPath];
   NSURL *accessToURL = [NSURL fileURLWithPath:[path stringByAppendingPathComponent:@"xx"]];
   if (@available(iOS 9.0, *)) {
        // 授权目录并加载
        [self.webView loadFileURL:url allowingReadAccessToURL:accessToURL];
   }

你可能感兴趣的:(iOS文件下载解压,WKWebView加载沙盒html)