美文网首页
加载本地HTML代码及资源文件的两种方式

加载本地HTML代码及资源文件的两种方式

作者: __Gavin__ | 来源:发表于2017-08-17 11:28 被阅读0次
添加文件
addHTML.png
1. 选择Create groups

此时HTML等资源文件不存在目录结构,对.css文件的引用需要如下格式:
href="main.css"
src="test.png"

加载代码如下:
NSString *path = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];
NSString *htmlString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
NSString *basePath = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:basePath];
[self.webView loadHTMLString:htmlString baseURL:baseURL];

2. 选择Create folder references

此时HTML等资源文件存在目录结构,对.css文件的引用需要如下格式:
href="css/main/main.css"
src="img/test.png"

加载代码如下:
UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
webView.delegate = self;
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"index" ofType:@"html" inDirectory:@"web/Stj"]]]];
[self.view addSubview:webView];

相关文章

网友评论

      本文标题:加载本地HTML代码及资源文件的两种方式

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