uiwebview加载本地html5文件,GitHub - MrLongZX/WebViewLoadLocal: UIWebView和WKWebView加载本地文件(html ,js,css,imag...

UIWebView和WKWebView加载本地文件(html ,js,css,image files)

一、UIWebView加载本地资源 (资源文件可以放到工程目录下也可以放到沙盒里面)

[webView loadHTMLString:## baseURL:[NSURL URLWithString:##]];

Demo

UIWebView *webView = [[UIWebView alloc]initWithFrame:CGRectMake(0 * self.view.bounds.size.width, 0, self.view.bounds.size.width, self.view.bounds.size.height-64)];

webView.delegate = self;

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *path = [paths objectAtIndex:0];

NSString *basePath = [NSString stringWithFormat:@"%@/%@",path,@"QueHTML/"];

NSString *htmlPath = [[NSBundle mainBundle] pathForResource:@"123" ofType:@"html"];

NSString *htmlString2 = [NSString stringWithContentsOfFile:htmlPath encoding:NSUTF8StringEncoding error:nil];

[webView loadHTMLString:htmlString2 baseURL:[NSURL URLWithString:basePath]];

二、WKWebView加载本地资源 (资源文件可以放到工程目录下也可以放到沙盒里面)

1、iOS8

iOS8 使用WKWebView加载带有js css和资源文件的html需要使用loadRequest

iOS8下使用

loadHTMLString:baseURL:

会出现加载报错的情况

This is what I learned about WKWebView, Apple's new WebKit API debuted on iOS 8.

As of this writing, the latest iOS version is iOS 8.1.3.

file:/// doesn't work without tmp directory

所以需要做一下处理: 把文件拷贝到tmp下面

//将文件copy到tmp目录

- (NSURL *)fileURLForBuggyWKWebView8:(NSURL *)fileURL {

NSError *error = nil;

if (!fileURL.fileURL || ![fileURL checkResourceIsReachableAndReturnError:&error]) {

return nil;

}

// Create "/temp/www" directory

NSFileManager *fileManager= [NSFileManager defaultManager];

NSURL *temDirURL = [[NSURL fileURLWithPath:NSTemporaryDirectory()] URLByAppendingPathComponent:@"www"];

[fileManager createDirectoryAtURL:temDirURL withIntermediateDirectories:YES attributes:nil error:&error];

NSURL *dstURL = [temDirURL URLByAppendingPathComponent:fileURL.lastPathComponent];

// Now copy given file to the temp directory

[fileManager removeItemAtURL:dstURL error:&error];

[fileManager copyItemAtURL:fileURL toURL:dstURL error:&error];

// Files in "/temp/www" load flawlesly :)

return dstURL;

}

Demo

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *path = [paths objectAtIndex:0];

NSString *basePath = [NSString stringWithFormat:@"%@/%@",path,@"QueHTML/"];

NSString *htmlPath = [NSString stringWithFormat:@"%@/%@",path,@"QueHTML/123.html"];

NSURL *fileURL = [self fileURLForBuggyWKWebView8:[NSURL fileURLWithPath:basePath]];

NSString *htmlString = [NSString stringWithContentsOfFile:[NSTemporaryDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"www/QueHTML/123.html"]] encoding:NSUTF8StringEncoding error:nil];//获取文件路径,现在html的文件路径已经改了。

WKWebView *webView = [[WKWebView alloc]initWithFrame:CGRectMake(0 * self.view.bounds.size.width, 0, self.view.bounds.size.width, self.view.bounds.size.height - 64)];

webView.navigationDelegate = self;

[self.view addSubview:webView];

[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent: [NSString stringWithFormat:@"www/QueHTML/123.html"]]]]];

2、iOS9~iOS11

iOS9~iOS11 使用这个API

loadFileURL:## allowingReadAccessToURL:##

Demo

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *path = [paths objectAtIndex:0];

NSString *basePath = [NSString stringWithFormat:@"%@/%@",path,@"QueHTML/"];

NSString *htmlPath = [NSString stringWithFormat:@"%@/%@",path,@"QueHTML/123.html"];

NSString *htmlString = [NSString stringWithContentsOfFile:htmlPath encoding:NSUTF8StringEncoding error:nil];//获取文件路径,现在html的文件路径已经改了。

WKWebView *webView = [[WKWebView alloc]initWithFrame:CGRectMake(0 * self.view.bounds.size.width, 0, self.view.bounds.size.width, self.view.bounds.size.height)];

webView.navigationDelegate = self;

[self.view addSubview:webView];

[webView loadFileURL:fileURL allowingReadAccessToURL:[NSURL fileURLWithPath:basePath isDirectory:YES]];

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值