UIWebView baseURL和绝对路径 [英] UIWebView baseURL and absolute path

查看:114
本文介绍了UIWebView baseURL和绝对路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个HTML页面,其结构如下:

I have an HTML page that is constructed like this:

<html>
  <head>
    <link rel="stylesheet" href="/style.css" />
  </head>
  <body>

  </body>
</html>

它存储在我的文档目录中:

It is stored in my documents directory:

/Users/username/Library/Application Support/iPhone Simulator/5.1/Applications/12345-ID/Documents/mysite/index.html

我还将style.css存储在documents目录中:

I have also stored the style.css in the documents directory:

/Users/username/Library/Application Support/iPhone Simulator/5.1/Applications/12345-ID/Documents/mysite/style.css

但是现在,当我尝试使用以下方法将html加载到我的web视图中时:

But now, when I try to load the html into my webview using this:

NSURL *test = [NSURL URLWithString:@"file:///Users/username/Library/Application Support/iPhone%20Simulator/5.1/Applications/12345-ID/Documents/mysite/"]
[myWebView loadHTMLString:<the content retrieved from index.html> baseURL:test];

当我使用NSURLProtocol截取对样式表的请求时,css没有加载请求是错误的。它正在尝试请求:

The css is not loaded, when I intercept the request to the stylesheet with a NSURLProtocol I can see the request is wrong. It is trying to request:


file:///style.css

file:///style.css

而不是完整的baseURL。现在,我可以通过删除html文件中/style.css前面的/来解决此问题。但是,有没有一种简便的方法可以在不修改HTML的情况下以本机代码解决此问题?

instead of the full baseURL. Now I can solve this by removing the / in front of /style.css in the html file. But is there an easy way to resolve this in native code without modifying the html?

推荐答案

唯一的解决方案是删除斜线

Removing the slash was the only solution

这篇关于UIWebView baseURL和绝对路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆