2014-01-06 39 views
0

我使用的Xcode 4.6試圖加載本地HTML文件中的IOS,但我不能加載它,它沒有出現在模擬器任何無法加載本地HTML文件中的IOS

我的代碼是

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"LocalPage" ofType:@"html"  inDirectory:nil]; 
    NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil]; 
    [self.webView loadHTMLString:htmlString baseURL:nil]; 
} 

任何人都可以告訴我這是什麼問題?

+2

你爲什麼要使用Xcode中的過時版本加載文件? – Mundi

+0

可能的重複[如何加載本地HTML文件到UIWebView?](http://stackoverflow.com/questions/4645414/how-can-i-load-a-local-html-file-into-the -uiwebview) – Mani

+1

此網頁視圖是否已添加到視圖層次結構中? –

回答

0

嘗試做一些錯誤檢查:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    NSError *error = nil; 
    NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"LocalPage" ofType:@"html" inDirectory:nil]; 
    NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:error]; 
    if(htmlString) 
     [self.webView loadHTMLString:htmlString baseURL:nil]; 
    else 
     NSLog(@"error in loading string from file %@ - %@", htmlFile, [error localizedDescription]); 
} 

這可能是.html文件不存在,你認爲它應該是,也許它是一個空文件。

另外,請利用您可以使用的「NSError」參數。

0

您可以通過URL

NSURL *htmlURL = [[NSBundle mainBundle] URLForResource:@"LocalPage" withExtension:@"html"]; 
[self.webView loadRequest:[NSURLRequest requestWithURL:htmlURL]]; 
相關問題