1. <tfoot id='2XnHT'></tfoot>
    2. <small id='2XnHT'></small><noframes id='2XnHT'>

        <i id='2XnHT'><tr id='2XnHT'><dt id='2XnHT'><q id='2XnHT'><span id='2XnHT'><b id='2XnHT'><form id='2XnHT'><ins id='2XnHT'></ins><ul id='2XnHT'></ul><sub id='2XnHT'></sub></form><legend id='2XnHT'></legend><bdo id='2XnHT'><pre id='2XnHT'><center id='2XnHT'></center></pre></bdo></b><th id='2XnHT'></th></span></q></dt></tr></i><div id='2XnHT'><tfoot id='2XnHT'></tfoot><dl id='2XnHT'><fieldset id='2XnHT'></fieldset></dl></div>
          <bdo id='2XnHT'></bdo><ul id='2XnHT'></ul>
        <legend id='2XnHT'><style id='2XnHT'><dir id='2XnHT'><q id='2XnHT'></q></dir></style></legend>

        iPhone Dev:UIWebView baseUrl 到 Documents 文件夹中的资源而不是 App 包

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

          • <small id='dFZTb'></small><noframes id='dFZTb'>

              <i id='dFZTb'><tr id='dFZTb'><dt id='dFZTb'><q id='dFZTb'><span id='dFZTb'><b id='dFZTb'><form id='dFZTb'><ins id='dFZTb'></ins><ul id='dFZTb'></ul><sub id='dFZTb'></sub></form><legend id='dFZTb'></legend><bdo id='dFZTb'><pre id='dFZTb'><center id='dFZTb'></center></pre></bdo></b><th id='dFZTb'></th></span></q></dt></tr></i><div id='dFZTb'><tfoot id='dFZTb'></tfoot><dl id='dFZTb'><fieldset id='dFZTb'></fieldset></dl></div>
              <legend id='dFZTb'><style id='dFZTb'><dir id='dFZTb'><q id='dFZTb'></q></dir></style></legend>

                  <tbody id='dFZTb'></tbody>
                <tfoot id='dFZTb'></tfoot>

                • <bdo id='dFZTb'></bdo><ul id='dFZTb'></ul>
                  本文介绍了iPhone Dev:UIWebView baseUrl 到 Documents 文件夹中的资源而不是 App 包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  您好!任何人都可以帮助我找到解决以下问题的方法:

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

                  • 使用 loadHTMLString 将 html 加载到 UIWebView 并包含(使用 baseURL)资源,例如 CSS、来自文件夹中的图像文件用户的 Documents 目录 - 并且不是来自应用程序的 MainBundle.
                  • 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.

                  我看过关于如何使用 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 包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Getting an NSArray of a single attribute from an NSArray(从 NSArray 获取单个属性的 NSArray)
                  ImageIO: lt;ERRORgt; JPEG Corrupt JPEG data: premature end of data segment iphone - how to catch this?(ImageIO:lt;错误gt;JPEG 损坏的 JPEG 数据:iphone 数据段过早结束 - 如何捕捉到这个?)
                  Xcode iOS organizer submit to app store yields quot;The archive is invalidquot; error(Xcode iOS 管理器提交到应用商店产生“存档无效;错误)
                  MFMessageComposeViewController alloc returns nil(MFMessageComposeViewController alloc 返回 nil)
                  Mobile Device Management with iPhone(使用 iPhone 进行移动设备管理)
                  iPhone: How to allow multiple selection in tabelview for a custom cell?(iPhone:如何允许在自定义单元格的表格视图中进行多项选择?)
                  • <small id='HcBXd'></small><noframes id='HcBXd'>

                    <tfoot id='HcBXd'></tfoot>
                    <legend id='HcBXd'><style id='HcBXd'><dir id='HcBXd'><q id='HcBXd'></q></dir></style></legend>
                      <bdo id='HcBXd'></bdo><ul id='HcBXd'></ul>

                        <i id='HcBXd'><tr id='HcBXd'><dt id='HcBXd'><q id='HcBXd'><span id='HcBXd'><b id='HcBXd'><form id='HcBXd'><ins id='HcBXd'></ins><ul id='HcBXd'></ul><sub id='HcBXd'></sub></form><legend id='HcBXd'></legend><bdo id='HcBXd'><pre id='HcBXd'><center id='HcBXd'></center></pre></bdo></b><th id='HcBXd'></th></span></q></dt></tr></i><div id='HcBXd'><tfoot id='HcBXd'></tfoot><dl id='HcBXd'><fieldset id='HcBXd'></fieldset></dl></div>
                          <tbody id='HcBXd'></tbody>