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

2019-04-16  本文已影响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];
}

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

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

下载完成后需要对于文件进行解压:对应的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

上一篇 下一篇

猜你喜欢

热点阅读