将需要的js、css资源导入创建好的bundle下,如上图
WKWebView *webView = [[WKWebView alloc]initWithFrame:frame]]
这个filePaths 就是需要加载的 js、css文件在本地的路径,是个数组,因为可能需要加载本地的多个js、css文件, 如果需要加载的js、css文件较多,可以让后台传给你对应js、css文件的路径,注意传的路径要跟导入本地的资源路径一致,否则会加载失败。
//路径path
NSArray *pathArray = dict[@"filePath"]
if (pathArray) {
NSError *error
//获取网络的HTML
NSString * online_HTML = [NSString stringWithContentsOfURL:[NSURL URLWithString:url] encoding:NSUTF8StringEncoding error:&error]
if(!error) {
//以分割
NSArray * array_HTML = [online_HTML componentsSeparatedByString:@"</head>"]
NSMutableString *header_HTML = [[NSMutableString alloc]initWithString:array_HTML.firstObject]
for (NSString *path in pathArray) {
//注意这里的hightcharts.bundle,更改成你本地的bundle名
NSString *filePath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"hightcharts.bundle/%@",path] ofType:nil]
if ([filePath hasSuffix:@"js"]) {
[header_HTML appendFormat:@"<script src=\"%@\"><\script>",filePath]
}else if ([filePath hasSuffix:@"css"]){
[header_HTML appendFormat:@"<link rel=\"stylesheet\"
[_webView loadHTMLString:header_HTML baseURL:[[NSBundle mainBundle] bundleURL]]
}
}
将图片文件、JS文件与你的网页文件保存在同一文件夹中,然后去掉图片的路径即可。即修改中间的代码为:runimg.imgurl = [ "<img src=’T1t8eTXbBtXXXXXXXX-490-170.png‘ />", "<img src=‘T1OVOUXeNjXXXXXXXX.jpg’ />", "<img src=’T1J.9TXc8lXXXXXXXX.jpg‘ />"]向本地写入数据的方法有以下几点:
1、ajax把要写的数据提交到后台后后台用io写 纯前台貌似正规途径不可能的样子。
2、如果写到服务器端呢。就是所谓的localhost:8080
3、ajax把要写的数据提交到后台后后台用io写 纯前台貌似正规途径不可能的样子,如图:
如果写到服务器端呢。就是所谓的localhost:8080
4、javasript能读取本地的json文件,但是无法写入,读取也本来就是hack的形式读取的,并不像服务端语言那样可以直接操作io,所以最好是后端语言结合使用操作io。