iOS 加载本地HTML网页 js css images等文件

开发环境:Xocde Version 7.0.1 (7A1001)

模拟器SDK:Version 9.0 (SimulatorApp-620 CoreSimulator-179)

项目需求:在进入一个新的页面时,webView展示本地HTML网页,并带js交互。

需要注意:调了一下午也没跳出来,错误原因就是往工程里拖要显示的html等资源时,选错了选项。

选creat groups ,运行时会崩溃:

reason: '*** -[NSURL initFileURLWithPath:]: nil string parameter'

代码部分:

    UIWebView *tmpWebView = [[UIWebView alloc] initWithFrame:self.view.bounds];

    [tmpWebView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"demo" ofType:@"html" inDirectory:@"chart"]]]];

    [self.view addSubview:tmpWebView];

 

三个参数的含义:

1、pathForResource:@"demo" :demo就是本地html的文件名

2、ofType:@"html" :不用多说了吧。文件类型

3、inDirectory:@"chart" :你拖到工程里的文件夹名字。

 

run,这样就可以加载出来了!

原文地址:https://www.cnblogs.com/sunflower-lhb/p/4998303.html