无法将本地 html 文件加载到 ios 设备(不是模拟器)上的 WKWebView [英] Cannot load local html file into WKWebView on ios device (not simulator)

查看:35
本文介绍了无法将本地 html 文件加载到 ios 设备(不是模拟器)上的 WKWebView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用以下代码时遇到问题.它应该将本地 HTML 文件加载到 WKWebView 中.

I'm having troubles with the following code. It's supposed to load a local HTML file into a WKWebView.

它在我尝试过的任何模拟器上都能正常工作(ios 12/ios 13).

It works fine on any simulator I've tried (ios 12 / ios 13).

由于我还不明白的原因,它在设备上不起作用.相反,我得到一个空白屏幕.使用 Safari 调试器调试 webview 显示页面是about:blank".它没有加载我的文件...为什么?

For a reason I do not understand yet, it does not work on device. Instead, I get a blank screen. Debugging the webview with the Safari debugger shows that the page is "about:blank". It did not load my file... Why ?

请注意,我已经通过从手机下载应用程序容器来检查文件是否存在,并且文件在那里...

Note that I have checked the existence of the file by downloading the app container from the phone and the file is there...

- (void)loadIndexConteneurWithHash:(NSString *)hash
{
    NSString *fileName = @"index.html";
    NSString *subpath = hash ? [NSString stringWithFormat:@"%@#%@", fileName, hash] : fileName;
    NSString *rootContainerDirectoryPath = [NSFileUtility pathRelativeToContentDirectoryForSubpath:@"/www/v2"];

    NSURL *URL = [NSURL URLWithString:subpath relativeToURL:[NSURL fileURLWithPath:rootContainerDirectoryPath]];

    [[NSURLCache sharedURLCache] removeAllCachedResponses];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10.0f];

    [self.webView performSelectorOnMainThread:@selector(loadRequest:) withObject:request waitUntilDone:true];
}

这个问题不是加载本地网络文件&WKWebView 中的资源

我没有从包中加载我的 HTML 文件,而是从我的问题中最初提到的应用程序容器中加载.我也试过 loadFileURL 方法,但它也不起作用.

I do not load my HTML file from the bundle but from the app container as noted originally in my question. I've also tried the loadFileURL method and it does not work either.

推荐答案

为了访问本地资源,您必须使用:Bundle.main.url(forResource: "MyFile", withExtension: "html")

In order to access a Local ressource you have to use : Bundle.main.url(forResource: "MyFile", withExtension: "html")

这篇关于无法将本地 html 文件加载到 ios 设备(不是模拟器)上的 WKWebView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆