您好!任何人都可以帮助我找到解决以下问题的方法:
Greetings! Can anyone please kindly assist me finding a way around the following:
我看过关于如何使用 baseURL 在应用程序包/MainBundle 中加载的教程,这很简单,但不适用于 iPhone 的 Documents 目录中的资源.
I have seen tutorials on how to use baseURL to load within the application bundle/MainBundle which is straightforward but not with resources from the iPhone's Documents directories.
我的文档文件夹结构如下:
The structure of my documents folder is as follows:
dirX
|---> file.xml
|---> dirCSS
|---> style.css
我可以检索到 dir X(Users/......./dir X) 的完整路径.但是,当将该路径传递给 UIWebView 的 baseURL 时,
I can retrieve the full path to the dir X(Users/......./dir X). However, when passing that path to the UIWebView's baseURL such that
[webView loadHTMLString:fileXMLString baseURL:pathToDirX]
... webView 无法将资源(例如 dirCSS 中的 style.css)识别为 fileXMLString 中的 href'ed
... webView does not recognize the resources(eg style.css within dirCSS) as href'ed within the fileXMLString
<link href="dirCSS/style.css" rel="stylesheet" />
所以目前我的应用程序可以成功加载 html 字符串但不加载样式表,因为 html 字符串中的 CSS 链接是 relative - 例如.css/style.css
So currently my application can successfully load the html string but does not load the stylesheet as the link to the CSS within the html string are relative - eg. css/style.css
非常感谢任何帮助:)
经过一番谷歌搜索,我终于找到了适合我提出的问题的解决方案.希望这能帮助那些面临同样问题的人.
After some googling I have finally found a fitting solution for the question I posed. Hopefully this would help those who face the same problem.
诀窍在于为 UIWebView 的 baseURL 创建 NSURL 对象时字符串路径的格式.虽然通常我在大多数情况下使用典型的Users/...../dir/file",但使用 UIWebView 的 loadHTMLString:baseURL 加载需要不同的方法.
The trick is with the formatting of the string path when creating an NSURL object for baseURL of a UIWebView. Although usually I use the typical "Users/...../dir/file" in most cases, loading using UIWebView's loadHTMLString:baseURL needs a different approach.
如 http://dblog.com 中所述.au/iphone-development/loading-local-files-into-uiwebview/,在那里我得到了解决方案,资源的字符串路径只需要用双斜杠替换斜杠,用 %20 替换空格:
As described in http://dblog.com.au/iphone-development/loading-local-files-into-uiwebview/, where I got the solution, string path to the resources just needs to have slashes to be replaced with double-slashes and spaces with %20:
NSString *imagePath = [[NSBundle mainBundle] resourcePath];
imagePath = [imagePath stringByReplacingOccurrencesOfString:@"/" withString:@"//"];
imagePath = [imagePath stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
NSString *HTMLData = @"
<h1>Hello this is a test</h1>
<img src="sample.jpg" alt="" width="100" height="100" />";
[webView loadHTMLString:HTMLData baseURL:
[NSURL URLWithString:
[NSString stringWithFormat:@"file:/%@//",imagePath]
]];
请注意字符串的替换以及:
Do take note of the replacing of the strings and also the:
[NSString stringWithFormat:@"file:/%@//",imagePath]
虽然上面的示例代码正在检索应用程序的 mainBundle 的路径,但它也可以在其他文件夹中工作,即 Documents(及其子文件夹),就像我在我的那样.
Although the example code above is retrieving the path to the mainBundle of the application, it can also work in other folders, ie Documents(and its subfolders) as I did in mine.
亲切的问候,呜呜呜
PS 再次感谢 Nic 的回复 :)
PS Thanks again Nic for the reply :)
这篇关于iPhone Dev:UIWebView baseUrl 到 Documents 文件夹中的资源而不是 App 包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!