美文网首页
iOS UIWebView 加载本地资源html/css/js/

iOS UIWebView 加载本地资源html/css/js/

作者: 三点水老木头 | 来源:发表于2018-01-10 12:00 被阅读39次
一、全部加载到根目录中

创建文件时放入根目录或者拖拽文件进项目时选中 Create groups 如图:


image.png

这时你的资源会全部加载在根目录中,这时你的html代码引用资源时不用添加路径,直接使用文件名,如

<script type='text/javascript' src='jquery.js' charset='utf-8'></script>
<script type='text/javascript' src='slip.min.js' charset='utf-8'></script>
<script type='text/javascript' src='index.js' charset='utf-8'></script>

二、按原文件目录结构加载

资源文件拖拽进项目时选择 Create folder references,如图


image.png

这里你引用资源文件就要使用原本的资源路径,如图


image.png
<script type='text/javascript' src='js/jquery.js' charset='utf-8'></script>
<script type='text/javascript' src='js/slip.min.js' charset='utf-8'></script>
<script type='text/javascript' src='js/index.js' charset='utf-8'></script>
iOS代码区别
- (UIWebView *)myWebView {
    if (_myWebView == nil) {
        _myWebView = [[UIWebView alloc] init];
        // 第一种
        NSString *path = [[NSBundle mainBundle] bundlePath];
        NSURL *baseURL = [NSURL fileURLWithPath:path];
        NSString * htmlPath = [[NSBundle mainBundle] pathForResource:@"index"
                                                              ofType:@"html"];
        NSString * htmlCont = [NSString stringWithContentsOfFile:htmlPath
                                                        encoding:NSUTF8StringEncoding
                                                           error:nil];
        [_myWebView loadHTMLString:htmlCont baseURL:baseURL];
    }
    return _myWebView;
}
- (UIWebView *)myWebView {
    if (_myWebView == nil) {
        _myWebView = [[UIWebView alloc] init];
       // 第二种
        [_myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"yxxTeacherload/index.html" relativeToURL:[[NSBundle mainBundle] bundleURL]]]];

    }
    return _myWebView;
}

相关文章

网友评论

      本文标题:iOS UIWebView 加载本地资源html/css/js/

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