【问题标题】:Can't load html file to webview无法将 html 文件加载到 webview
【发布时间】:2009-11-11 10:31:53
【问题描述】:

这是我的代码:

NSString *path1 = [[[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 中的输出如下: 像

........

它显示页面源代码,而不是让我正确查看该文件。

请帮忙... 提前致谢

【问题讨论】:

  • 我正在从资源中加载这个 html 文件。

标签: iphone


【解决方案1】:

首先首选 pathForRessource ofType

pathNSString *path1 = [[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]]];

【讨论】:

    【解决方案2】:
    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];
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-10
      • 1970-01-01
      • 2019-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多