2012-12-10 48 views
0

我有一個web應用程序,它的所有資源都包含在一個名爲「war」的文件夾中。我將它們導入到我的xcode項目中(通過File - >「Add Files to'my項目'」)。從本地war文件夾加載html文件

現在我的Xcode項目的樣子:

MyProject 
    |- war 
     |- foo.html 
    |- AppDelegate.h 
    |- AppDelegate.m 
    ... 

所以現在我嘗試加載像:

NSBundle *bundle = [NSBundle mainBundle]; 
NSURL *url = [NSURL fileURLWithPath:[bundle pathForResource:@"foo" ofType:@"html"] isDirectory:NO]; 
NSURLRequest *req = [NSURLRequest requestWithURL:url]; 
[self.mWebView loadRequest:req]; 

我沒有得到編譯錯誤,但我的UIWebView是空的。我也得到了webViewDidLoad回調。有沒有其他的方式來加載這樣的本地網頁?

感謝

+0

如果你保存的目錄名稱,使用[URLForResource:withExtension:子目錄:(https://developer.apple.com/library/mac/documentation /Cocoa/Reference/Foundation/Classes/NSBundle_Class/Reference/Reference.html#//apple_ref/occ/instm/NSBundle/URLForResource:withExtension:subdirectory :)。如果文件夾已轉換爲項目中的組,則只需使用[URLForResource:withExtension:](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSBundle_Class/Reference /Reference.html#//apple_ref/occ/instm/NSBundle/URLForResource:withExtension :)。 – Rob

+0

嗨,我認爲dir的名字被保存下來,沒有任何修改。出於某種原因,xcode抱怨你列出的第一種方法,稱它沒有找到。我可以得到「URLForResource:withExtension:subdirectory:inBundleWithURL:」來編譯,但我不確定「inBundleWithURL」使用什麼。你列出的不應該編譯好嗎?它說10.6及以上... ... – user291701

+0

'NSURL * url = [[NSBundle mainBundle] URLForResource:@「foo」withExtension:@「html」subdirectory:@「war」];'適合我。 – Rob

回答

4

如果它是一個真正的子目錄(即文件夾名稱以藍色顯示出來):

enter image description here

然後你可以使用URLForResource:withExtension:subdirectory:

NSURL *url = [[NSBundle mainBundle] URLForResource:@"foo" 
            withExtension:@"html" 
             subdirectory:@"war"]; 

或者,如果它只是一個「組「(即如果」文件夾「不顯示爲藍色):

enter image description here

然後使用普通的老URLForResource:withExtension:

NSURL *url = [[NSBundle mainBundle] URLForResource:@"foo" 
            withExtension:@"html"]; 
+0

謝謝你的詳細解答。我很難讓這個工作。我不得不使用「爲所有添加的文件夾創建文件夾引用」選項添加文件。然後我使用上面列出的第一種方法。用「爲所有添加的文件夾創建組」文件導入文件似乎不起作用。謝謝 – user291701

0

這是怎麼了,我通常加載本地HTML文件沒有問題。

[webView loadRequest:[NSURLRequest requestWithURL:[[NSBundle mainBundle] URLForResource:self.fileName withExtension:nil]]] 

由於你的代碼大多看起來是正確的我認爲這可能是一些其他問題。我建議爲你的webView設置一個委託並檢查它的方法,看看它是否提供了更多信息。

還請確保您的foo.html是目標成員。