无法将html文件加载到webview

时间:2021-06-23 19:54:11

Here is my code :

这是我的代码:

NSString *path1 = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"NewSouthWales.html"];

NSString *path1 = [[NSBundle mainBundle 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];

I am getting output in my webview as : like

我在webview中得到的输出是:like

........

........

instead of giving me proper look of that file it shows page source code.

而不是给我正确的文件,它显示了页面源代码。

Please help ... Thanks in advance

请帮助……谢谢提前

2 个解决方案

#1


0  

First prefer pathForRessource ofType

第一次喜欢pathForRessource减低

pathNSString *path1 = [[NSBundle mainBundle] pathForResource:@"NewSouthWales" ofType:@"html"];

pathNSString *path1 = [NSBundle mainBundle] pathForResource:@“NewSouthWales”,类型:@“html”;

And then if the file is local why send a request ? Just use :

如果文件是本地的,为什么发送请求?只使用:

NSString *html = [NSString stringWithContentsOfFile:path1 encoding:NSUTF8StringEncoding error:nil];
[self.webView loadHTMLString:html baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle]bundlePath]]];

#2


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];
  }

#1


0  

First prefer pathForRessource ofType

第一次喜欢pathForRessource减低

pathNSString *path1 = [[NSBundle mainBundle] pathForResource:@"NewSouthWales" ofType:@"html"];

pathNSString *path1 = [NSBundle mainBundle] pathForResource:@“NewSouthWales”,类型:@“html”;

And then if the file is local why send a request ? Just use :

如果文件是本地的,为什么发送请求?只使用:

NSString *html = [NSString stringWithContentsOfFile:path1 encoding:NSUTF8StringEncoding error:nil];
[self.webView loadHTMLString:html baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle]bundlePath]]];

#2


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];
  }