2009-11-11 66 views
0

這裏是我的代碼:無法加載HTML文件的WebView

的NSString *路徑1 = [[[一個NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@ 「NewSouthWales.html」];

NSURL *pageURL = [NSURL fileURLWithPath:path1]; 
//URL Requst Object 
NSURLRequest *requestObj = [NSURLRequest requestWithURL:pageURL]; 

//Load the request in the UIWebView. 
[self.webView loadRequest:requestObj]; 

我得到的輸出在我的WebView爲: 像

........

,而不是給我的文件它顯示正確的外觀頁面源代碼。

請幫助... 在此先感謝

+0

我加載從資源這個HTML文件。 – Nic 2009-11-11 10:33:49

回答

0

首先喜歡pathForRessource ofType

pathNSString *路徑1 = [[一個NSBundle mainBundle] pathForResource:@ 「NewSouthWales」 ofType:@ 「HTML」];

然後如果文件是本地的,爲什麼發送請求? 只需使用:

NSString *html = [NSString stringWithContentsOfFile:path1 encoding:NSUTF8StringEncoding error:nil]; 
[self.webView loadHTMLString:html baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle]bundlePath]]]; 
0
NSString *path1 = [[NSBundle mainBundle] pathForResource:@"NewSouthWales" ofType:@"html"]; 
    NSURL *baseurl = [NSURL fileURLWithPath:path1]; 

    NSData *pathData = [NSData dataWithContentsOfFile:path1]; 
//Check first if the file path's data is captured. if true, load the html on the web view 

    if (pathData) { 
     [webView loadData:pathData MIMEType:@"text/html" textEncodingName:@"UTF-8" baseURL:baseurl]; 
    } 
相關問題