首页 文章

使用uiwebview中的本地html从相对路径加载资源

提问于
浏览
120

我有一个非常简单的iOS应用程序,带有uiwebview加载一个非常简单的测试页面(test.html):

<html>
<body>
<img src="img/myimage.png" />
</body>
</html>

我将此test.html文件加载到我的Web视图中:

NSURL *url = [[NSBundle mainBundle] URLForResource:@"test" withExtension:@"html"];
NSString *html = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
NSURL *baseUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
[webView loadHTMLString:html baseURL:baseUrl];

如果我在没有相对路径的情况下引用图像并将引用的图像放在根目录下的根目录下 - >在XCode中复制捆绑资源,这可以正常工作,但我不能让它与我的html文件中显示的相对路径一起工作 . 必须有一种方法可以做到这一点,我有很多图像,CSS,javascript文件,我想加载到webview中,我希望不必拥有根目录中的所有内容,并且必须更改我的网站中的所有引用应用程序 .

8 回答

  • 0

    这是如何加载/使用具有相对引用的本地html .

    • 将资源拖到你的xcode项目中(我从我的finder窗口拖动了一个名为www的文件夹),你将得到两个选项"create groups for any added folders"和"create folders references for any added folders" .

    • 选择"create folder references.."选项 .

    • 使用下面给出的代码 . 它应该像魅力一样工作 .

    NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"index" ofType:@"html" inDirectory:@"www"]]; [webview loadRequest:[NSURLRequest requestWithURL:url]];

    现在,html中的所有相关链接(如img / .gif,js / .js)都应该得到解决 .

    Swift 3

    if let path = Bundle.main.path(forResource: "dados", ofType: "html", inDirectory: "root") {
            webView.load( URLRequest(url: URL(fileURLWithPath: path)) )
        }
    
  • 0

    在Swift中:

    func pathForResource(  name: String?, 
                            ofType ext: String?, 
                            inDirectory subpath: String?) -> String?  {
    
      // **name:** Name of Hmtl
      // **ofType ext:** extension for type of file. In this case "html"
      // **inDirectory subpath:** the folder where are the file. 
      //    In this case the file is in root folder
    
        let path = NSBundle.mainBundle().pathForResource(             "dados",
                                                         ofType:      "html", 
                                                         inDirectory: "root")
        var requestURL = NSURL(string:path!)
        var request = NSURLRequest(URL:requestURL)
    
        webView.loadRequest(request)
    }
    
  • 5

    我把所有东西塞进了一条线(我知道不好)并且没有任何麻烦:

    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"test" 
                                                                                                             ofType:@"html"]
                                                                 isDirectory:NO]]];
    
  • 285

    我只是这样做:

    UIWebView *webView = [[[UIWebView alloc] init] autorelease];
    
        NSURL *url = [[NSBundle mainBundle] URLForResource:@"index" withExtension:@"html"];
        NSURLRequest* request = [NSURLRequest requestWithURL:url];
        [webView loadRequest:request];
    

    其中“index.html”相对引用图像,CSS,javascript等 .

  • 5

    迅速回答2 .

    UIWebView Class Reference建议不要使用webView.loadRequest(request):

    不要使用此方法加载本地HTML文件;相反,使用loadHTMLString:baseURL: .

    在此解决方案中,html被读入字符串 . html的url用于计算路径,并将其作为基本URL传递 .

    let url = bundle.URLForResource("index", withExtension: "html", subdirectory: "htmlFileFolder")
    let html = try String(contentsOfURL: url)
    let base = url.URLByDeletingLastPathComponent
    webView.loadHTMLString(html, baseURL: base)
    
  • -1

    我已经回去测试并重新测试了这个,似乎我可以让它工作的唯一方法(因为我在img文件夹中有一些文件,有些在js / css等等...)不是在html中使用我的图像的相对路径,并将所有内容引用到bundle文件夹 . 多可惜 :( .

    <img src="myimage.png" />
    
  • 23

    @ sdbrain在Swift 3中的回答:

    let url = URL.init(fileURLWithPath: Bundle.main.path(forResource: "index", ofType: "html", inDirectory: "www")!)
        webView.loadRequest(NSURLRequest.init(url: url) as URLRequest)
    
  • 2

    在使用WKWebView的Swift 3.01中:

    let localURL = URL.init(fileURLWithPath: Bundle.main.path(forResource: "index", ofType: "html", inDirectory: "CWP")!)
    myWebView.load(NSURLRequest.init(url: localURL) as URLRequest)
    

    这可以调整3.01中一些更精细的语法更改并保持目录结构,以便您可以嵌入相关的HTML文件 .

相关问题