我有一个
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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。