美文网首页
iOS下载Html文件包(包括视频,css,js.png) Tm

iOS下载Html文件包(包括视频,css,js.png) Tm

作者: 子小每文 | 来源:发表于2019-04-16 13:19 被阅读0次

1:首先如果放到本地目录下进行播放满足当前条件:(相对路径)
2:一般情况下直接打包放入appstore,由于包太大了下载和使用的情况,推广等各种问题会受到影响.
以下方案:本地下载然后加载对应的Html文件。
后台必须配置.zip文件,包名和下载的必须一致,前端进行解压,然后寻找对应的目录进行读取.html文件.
下面是对应下载代码:
<NSURLSessionDelegate>代理别忘记
注意:里面下载的地址就是tmp地址
-(void)downLoadHtml{
NSURLSessionConfiguration *cfg = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:cfg delegate:self delegateQueue:[NSOperationQueue mainQueue]];
NSURL *url = [NSURL URLWithString:@"http://admin-flv.kgc.cn/Storyline/www.zip"];
NSURLSessionDownloadTask *task = [session downloadTaskWithURL:url];
[task resume];
}

  • (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask
    didFinishDownloadingToURL:(NSURL *)location{
    // 创建存储文件路径
    NSString *caches = [NSString stringWithFormat:@"%@WebKit",NSTemporaryDirectory()];
    // response.suggestedFilename:建议使用的文件名,一般跟服务器端的文件名一致

    NSString file = [caches stringByAppendingPathComponent:downloadTask.response.suggestedFilename];
    NSLog(@"下载完成1111111");
    /
    *将临时文件剪切或者复制到Caches文件夹
    AtPath :剪切前的文件路径
    toPath :剪切后的文件路径
    */
    NSFileManager *mgr = [NSFileManager defaultManager];
    [mgr moveItemAtPath:location.path toPath:file error:nil];
    NSLog(@">>>>>>>>>>>>>>>>>%@",location.path);
    }

/**
每当下载完一部分时就会调用(可能会被调用多次)
参数:
bytesWritten 这次调用下载了多少
totalBytesWritten 累计写了多少长度到沙盒中了
totalBytesExpectedToWrite 文件总大小
*/

  • (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask
    didWriteData:(int64_t)bytesWritten
    totalBytesWritten:(int64_t)totalBytesWritten
    totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite{
    // 这里可以做些显示进度等操作
    NSLog(@">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>%lld",bytesWritten);
    self.progressView.progress = 1.0 * totalBytesWritten/totalBytesExpectedToWrite;
    self.labelProgress.text = [NSString stringWithFormat:@"%lf",self.progressView.progress];
    }

/**
恢复下载时使用
*/

  • (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask
    didResumeAtOffset:(int64_t)fileOffset
    expectedTotalBytes:(int64_t)expectedTotalBytes
    {
    // 用于断点续传
    }

下载完成后需要对于文件进行解压:对应的tmp文件路径
NSString *caches = [NSString stringWithFormat:@"%@WebKit/",NSTemporaryDirectory()];
NSString *file = [caches stringByAppendingPathComponent:@"www.zip"];
bundlePath];
BOOL is = [SSZipArchive unzipFileAtPath:file toDestination:caches];
if (is == YES) {
NSLog(@"解压成功");
}else{
NSLog(@"解压失败");
}
SSZipArchive是开源的第三方库,进行解压,压缩,和加密等情况处理。
下面代码方法是进行代码读取本地的Html文件;
self.webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0,SCREENWIDTH, SCREENHEIGHT)];
//添加到view中
self.webView.backgroundColor = [UIColor redColor];
[self.view addSubview: self.webView];
//沙盒路径:
NSLog(@"沙盒路径%@",NSHomeDirectory());
NSLog(@"NSHomeDirectory = %@",NSTemporaryDirectory());
NSString *sttr = [NSString stringWithFormat:@"%@WebKit/www/story.html",NSTemporaryDirectory()];
NSURL *feedbackURL = [NSURL fileURLWithPath:sttr];
[self.webView loadRequest:[NSURLRequest requestWithURL:feedbackURL]];

gitHub有文件包进行下载:
https://github.com/sunmin001/sunmin.git

相关文章

网友评论

      本文标题:iOS下载Html文件包(包括视频,css,js.png) Tm

      本文链接:https://www.haomeiwen.com/subject/edfhwqtx.html