我有一個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回調。有沒有其他的方式來加載這樣的本地網頁?
感謝
如果你保存的目錄名稱,使用[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
嗨,我認爲dir的名字被保存下來,沒有任何修改。出於某種原因,xcode抱怨你列出的第一種方法,稱它沒有找到。我可以得到「URLForResource:withExtension:subdirectory:inBundleWithURL:」來編譯,但我不確定「inBundleWithURL」使用什麼。你列出的不應該編譯好嗎?它說10.6及以上... ... – user291701
'NSURL * url = [[NSBundle mainBundle] URLForResource:@「foo」withExtension:@「html」subdirectory:@「war」];'適合我。 – Rob