2009-11-11 129 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]; 
    }