2009-12-18 51 views
19

問候!任何人都可以請您幫助我發現圍繞以下幾個辦法:從文件夾iPhone Dev:UIWebView baseUrl到Documents文件夾中的資源不是應用程序包

  • 負載的HTML成的UIWebView使用loadHTMLString,包括(使用基本URL)的資源,如CSS,圖像文件在用戶的文檔目錄中 - 和不是來自應用程序的MainBundle

我見過關於如何使用baseURL加載應用程序捆綁包/ MainBundle的教程,該應用程序捆綁包/ MainBundle非常簡單,但沒有來自iPhone的Documents目錄的資源。

我的文檔文件夾的結構如下:

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

我可以檢索的完整路徑的目錄X(用戶/......./ DIR X)。然而,通過這條道路的時候到一個UIWebView的基本URL,從而

[webView loadHTMLString:fileXMLString baseURL:pathToDirX] 

... webView的不承認的資源(例如在dirCSS style.css文件)作爲fileXMLString

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

內href'ed所以目前我的應用程序可以成功加載html字符串,但不加載樣式表,因爲鏈接到html字符串內的CSS是相對 - 例如。 CSS/style.css中

任何幫助是非常讚賞:)

回答

25

經過一番谷歌搜索後,我終於找到了一個適合我提出的問題的解決方案。希望這可以幫助那些面臨同樣問題的人。

訣竅是在爲UIWebView的baseURL創建NSURL對象時爲字符串路徑設置格式。儘管通常我在大多數情況下使用典型的「Users /...../ dir/file」,但使用UIWebView的loadHTMLString:baseURL進行加載需要不同的方法。

http://dblog.com.au/iphone-development/loading-local-files-into-uiwebview/,在那裏我得到了解決描述,路徑字符串的資源只是需要有斜槓被替換爲雙斜槓和空格%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] 
      ]]; 

待辦事項注意的替換字符串,並且還的:

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

儘管示例代碼上面檢索的路徑的應用的mainBundle,它也可以在其他文件夾中的工作,即,文檔(和其子文件夾)和我一樣在礦。

親切的問候, oonoo

PS再次感謝尼克的答覆:)

+1

您可以使用NSString的stringByAddingPercentEscapesUsingEncoding:方法添加轉義百分比。 – ddnv 2011-02-03 16:53:17

+0

這篇文章對我非常有幫助。這次真是萬分感謝! ;-) – andi1984 2012-01-26 11:25:51

+0

這是從應用程序包加載,而不是從Documents目錄加載。您鏈接的頁面也在討論從包中加載。 – 2012-06-26 23:40:21

12

要獲得路徑文件目錄:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask ,YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"index.xml"]; 
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]]] 

擁有一切工作的最簡單的方法,是簡單地加載從文檔目錄中的index.html而不是加載一個字符串。否則,將baseUrl設置爲文檔目錄。

+0

感謝尼古拉的及時答覆!我已經嘗試過使用該文檔目錄路徑,但UIWebView的baseURL似乎並沒有正確使用,並且不會從文檔目錄加載CSS /圖像。 其實我有兩件事 – oonoo 2009-12-18 03:44:07

+0

是的,事實上我沒有問題直接用loadRequest加載html,因爲它很容易放置圖像和CSS很好,但它的速度有問題。由於我從iPhone文檔文件夾中加載loadHTMLString似乎是理想的,實際上很容易加載html字符串,除了..是的,無法獲得加載的CSS和圖像。 – oonoo 2009-12-18 04:09:19

+0

我編輯了我的答案,嘗試loadRequest。 – 2009-12-18 04:36:24

1
// call this method and set the directory , Currently it is cache directory . If you want document directory then change NSCacheDirectory to NSDocumentDirectory 

-(NSString*)getBasePath 
{ 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, 
                NSUserDomainMask, YES); 
NSMutableString *cacheDirectory = [NSMutableString stringWithString:[paths objectAtIndex:0]]; 

[cacheDirectory appendString:@"/"]; 
NSRange renge= NSMakeRange(0, cacheDirectory.length); 
[cacheDirectory replaceOccurrencesOfString:@"/" withString:@"//" options:NSCaseInsensitiveSearch range:renge]; 
renge= NSMakeRange(0, cacheDirectory.length); 
[cacheDirectory replaceOccurrencesOfString:@" " withString:@"%20" options:NSCaseInsensitiveSearch range:renge]; 
[cacheDirectory insertString:@"file://" atIndex:0]; 
//file:////Users//hb//Library//Application%20Support//iPhone%20Simulator//6.0//Applications//3A141D33-390A-4DD2-8825-7DDC07D29894//Library//Caches// 
return cacheDirectory; 

}

相關問題