iOS项目中如何正确引入Html5文件(html/js/css)

iOS项目中使用项目中的html js css 文件时,有时会遇到引用路径出错的问题,导致html js css image文件无法加载的情况。

那么,引入H5相关文件的正确操作方式如下,这样就不会出现资源文件无法引入并正常使用的情况:

1 在项目目录下创建存放Html5文件文件夹:H5

2 将文件夹H5拖放至项目中对应的位置,此处注意要选择:Create groups

3 将各种Html5文件(html js css image等)拖放到H5文件夹中,此处注意选择:Create folder references

PS:黄色为groups , 蓝色为folder

4 UIWebView/WKWebView中的代码如下:

1 //创建要打开的html文件的完整路径
2     NSBundle *bundle = [NSBundle mainBundle];
3     NSString *resPath = [bundle resourcePath];
4     NSString *filePath = [resPath stringByAppendingPathComponent:@"index.html"];
5     //初始化一个UIWebView实例
6     myWebView = [[UIWebView alloc] initWithFrame:self.view.frame];
7     //加载指定的html文件
8     [myWebView loadRequest:[[NSURLRequest alloc] initWithURL:[[NSURL alloc] initFileURLWithPath:filePath]]];
9     [self.view addSubview:myWebView];

此时已经成功添加了Html5文件到项目中,并在UIWebView中展示出来了。

 

相关内容可以参考:http://blog.csdn.net/fanjunxi1990/article/details/9352917

posted @ 2016-11-01 12:46  jaysonguo  阅读(5410)  评论(0编辑  收藏  举报