加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 资源网站 > 资源 > 正文

html – UIWebView baseURL和绝对路径

发布时间:2020-12-14 18:45:27 所属栏目:资源 来源:网络整理
导读:我有一个 HTML页面,构造如下: html head link rel="stylesheet" href="/style.css" / /head body /body/html 它存储在我的文档目录中: /Users/username/Library/Application Support/iPhone Simulator/5.1/Applications/12345-ID/Documents/mysite/index.h
我有一个 HTML页面,构造如下:
<html>
  <head>
    <link rel="stylesheet" href="/style.css" />
  </head>
  <body>

  </body>
</html>

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

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

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

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

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

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];

没有加载css,当我用NSURLProtocol拦截样式表的请求时,我可以看到请求是错误的.它试图要求:

file:///style.css

而不是完整的baseURL.现在我可以通过删除html文件中的/style.css前面的/来解决这个问题.但有没有一种简单的方法可以在本机代码中解决这个问题而无需修改html?

解决方法

以下列方式生成您的路径和基本URL:
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
[webView loadHTMLString:htmlString baseURL:baseURL];

关于这个主题的精彩博客:

http://iphoneincubator.com/blog/windows-views/uiwebview-revisited

关于这个问题的SO QA

UIWebView and local css file

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读