iPhone Dev:UIWebView baseUrl到Documents文件夹中的资源而不是App bundle [英] iPhone Dev: UIWebView baseUrl to resources in Documents folder not App bundle

查看:118
本文介绍了iPhone Dev:UIWebView baseUrl到Documents文件夹中的资源而不是App bundle的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问候!任何人都可以请我帮忙找到解决方法:




  • 使用<将加载到 UIWebView 中em> loadHTMLString 并包含(使用 baseURL )资源,例如CSS,来自用户 Documents 目录中文件夹的图像文件 - 以及不来自应用程序的MainBundle



我见过有关如何在应用程序包/ MainBundle中加载baseURL的教程,这很简单,但没有来自iPhone的资源文件目录。



我的文件夹的结构如下:

  dirX 
| ---> file.xml
| ---> dirCSS
| ---> style.css

我可以检索dir X的完整路径(Users / ..... ../dir X)。但是,将该路径传递给UIWebView的baseURL时,

  [webView loadHTMLString:fileXMLString baseURL:pathToDirX] 

... webView无法识别资源(例如dirCSS中的style.css)作为fileXMLString中的href'

 < link href =dirCSS / style.css =stylesheet/> 

所以目前我的应用程序可以成功加载html字符串但不加载样式表作为链接到html字符串中的CSS是 relative - 例如。 css / style.css



非常感谢任何帮助:)

解决方案

经过一些谷歌搜索后,我终于找到了一个适合我提出的问题的解决方案。希望这可以帮助那些面临同样问题的人。



诀窍是为UIWebView的baseURL创建NSURL对象时格式化字符串路径。虽然在大多数情况下我通常使用典型的Users /...../dir / file,但使用UIWebView的loadHTMLString:baseURL加载需要一种不同的方法。



http://dblog.com.au/iphone-development/loading-local-files-into-uiwebview/ ,我得到了解决方案,资源的字符串路径只需要斜杠到用%s替换为双斜杠和空格:

  NSString * imagePath = [[NSBundle mainBundle] resourcePath]; 
imagePath = [imagePath stringByReplacingOccurrencesOfString:@/withString:@//];
imagePath = [imagePath stringByReplacingOccurrencesOfString:@withString:@%20];

NSString * HTMLData = @
< h1>你好,这是一个测试< / h1>
< img src =sample.jpgalt =width =100height =100/>;
[webView loadHTMLString:HTMLData baseURL:
[NSURL URLWithString:
[NSString stringWithFormat:@file:/% @ //,imagePath]
]];

请注意替换字符串以及:

  [NSString stringWithFormat:@file:/% @ //,imagePath] 

虽然上面的示例代码是检索应用程序的mainBundle的路径,但它也可以在其他文件夹中工作,即Documents(及其子文件夹),就像我在我的文件中一样。 / p>

亲切的问候,
oonoo



PS再次感谢Nic回复:)


Greetings! Can anyone please kindly assist me finding a way around the following:

  • load an html into a UIWebView using loadHTMLString and include(using baseURL) the resources such as the CSS, image files from folders within user's Documents directory - and not from the MainBundle of the application.

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

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 does not recognize the resources(eg style.css within dirCSS) as href'ed within the fileXMLString

<link href="dirCSS/style.css" rel="stylesheet" />

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

Any help is very much appreciated :)

解决方案

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.

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.

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]

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.

Kind regards, oonoo

PS Thanks again Nic for the reply :)

这篇关于iPhone Dev:UIWebView baseUrl到Documents文件夹中的资源而不是App bundle的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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