2010-06-04 108 views
0

從我收集的信息中,使用顯示本地html文件的UIWebView可以在圖像周圍環繞文本。 (我有一個名爲index.html的本地html文件)iPhone:圍繞圖像環繞文字

插入viewDidLoad方法中的以下代碼似乎會導致應用程序出錯並崩潰。 *** Terminating app due to uncaught exception 'NSInvalidArgumentException'

這個例子可能看起來很熟悉。這似乎是谷歌搜索中反覆出現的例子。如果有人知道另一種方式,例如,或爲什麼它崩潰的原因,請讓我知道。

[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"] isDirectory:NO]]]; 

請注意,「web視圖」是一個UIWebView的一個IBOutlet,我是能夠將HTML字符串加載到它;只是不是一個文件。此外,html文件的名稱正如「path.html」中所述,正如pathForResource和ofType中所述。

回答

1

這是我做的,裏面有更多的錯誤處理和東西:

NSError *error = nil; 
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"index" 
    ofType:@"html"]; 
html = [NSString stringWithContentsOfFile:filePath 
    encoding:NSUTF8StringEncoding error:&error]; 
if (error != nil) 
    NSLog (@"Error loading index.html: %@", [error localizedDescription]); 

NSString *basePath = [[NSBundle mainBundle] resourcePath]; 
NSURL *baseURL = [NSURL fileURLWithPath:basePath]; 
[webView loadHTMLString:html baseURL:baseURL]; 
+0

這是一個更清潔的方式。對於遇到此問題的任何人,請記得將您的html導入到您的項目中。這是我原來的問題。感謝lucius的幫助。 – 2010-06-07 13:27:11