【问题标题】:UIWebView not loading local .html fileUIWebView 未加载本地 .html 文件
【发布时间】:2013-07-02 15:57:10
【问题描述】:

我试图查看存储在我的 Bundle(在我的支持文件中)中的 .html 文件 (index.html)。

.html 文件位于名为 HTML 的文件夹中。我的代码如下:

- (void)viewDidLoad
    {
     [super viewDidLoad];  

  _viewWeb.delegate = self;

    NSString *path = [[NSBundle mainBundle]
                      pathForResource:@"index" ofType:@"html" inDirectory:@"HTML"];
    NSURL *url = [NSURL fileURLWithPath:path];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [_viewWeb setScalesPageToFit:YES];
    [self.viewWeb loadRequest:request];

    }

我的头文件如下:

@interface D6ViewController : UIViewController <UIWebViewDelegate>


    {

    IBOutlet UIWebView *viewWeb;

    }

@property (weak, nonatomic) IBOutlet UIWebView *viewWeb;

@end

我将属性合成为viewWeb = _viewWeb。持有 UIWebView 的视图控制器加载正常,但显示没有网页的白屏。我已经在IB设置了网点。

有什么想法吗?谢谢,

【问题讨论】:

标签: ios uiwebview


【解决方案1】:

您正在使用相对路径(因此文件夹的颜色为蓝色)。您实际上可以在这里Load resources from relative path using local html in uiwebview 或以下找到此问题的答案:

将资源拖到您的 xcode 项目中,您将获得两个选项“为任何添加的文件夹创建组”和“为任何添加的文件夹创建文件夹引用”。选择“创建文件夹引用..”选项。

下面的代码应该可以工作。

  NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"index" ofType:@"html" inDirectory:@"/HTML"]];
  [webView loadRequest:[NSURLRequest requestWithURL:url]];

【讨论】:

  • 还是什么都没有。只是白屏
  • 清理并重建。还要从您的模拟器/设备中删除该应用程序,并让它读取。文件可能没有正确复制。
  • 还是什么都没有。太奇怪了。我有另一个应用程序可以完美运行。一切都设置相同。
  • 删除您的资源并阅读它。您是否尝试从捆绑包中加载其他 html 文件?也许索引很时髦。
【解决方案2】:

试试这个

[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"]isDirectory:NO]]];

【讨论】:

    【解决方案3】:

    在斯威夫特中:

    func pathForResource(name: String?, ofType ext: String?, inDirectory subpath: String?) -> String?
    - 名称: Hmtl 的名称;
    - ofType ext: 文件类型的扩展名。在这种情况下,“html”;
    - inDirectory 子路径: 文件所在的文件夹。在这种情况下,文件位于根文件夹中;

        let path = NSBundle.mainBundle().pathForResource("dados", ofType: "html", inDirectory: "root")
        var requestURL = NSURL(string:path!);
        var request = NSURLRequest(URL:requestURL);
    
        webView.loadRequest(request)
    

    【讨论】:

      【解决方案4】:

      你错了,这里是正确的代码:

              var requestURL = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("index", ofType: "html", inDirectory: "www")!)
          var request = NSURLRequest(URL:requestURL!)
      
          myWebView.loadRequest(request)
      

      【讨论】:

        【解决方案5】:

        如果您不将 HTML 文件保存在任何目录中,则可以使用此选项。如果您将 HTML 文件保存在任何目录中,则在“inDirectory”参数中指定目录名称。

        NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"privacy-Policy" ofType:@"html" inDirectory:nil];
        NSData *htmlData = [NSData dataWithContentsOfFile:htmlFile];
        [_webView loadData:htmlData MIMEType:@"text/html" textEncodingName:@"UTF-8" baseURL:[NSURL URLWithString:@""]];
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-01-02
          • 2011-06-06
          • 2013-01-18
          • 2012-12-08
          • 2011-10-27
          相关资源
          最近更新 更多