无法将本地 html 文件加载到设备上的 WKWebView 中(适用于模拟器) [英] Cannot load a local html file into a WKWebView on device only (works on simulator)

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

问题描述

下面的本地代码将本地 HTML 文件(来自应用容器,而不是应用程序包)加载到 WKWebView 中.

The local code below loads into a WKWebView a local HTML file (from the app container, not the app bundle).

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

    [self.webView loadFileURL:URL allowingReadAccessToURL:URL];



    // This below has the same effect (works on simulator and not on device)

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

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

设备上的文件 url 是:file:///var/mobile/Containers/Data/Application/3CF58895-866D-400A-B0E0-4CA98BF143F7/Library/Application%20Support//LocalContents/www/v2/indexConteneur.html

The file url on device is : file:///var/mobile/Containers/Data/Application/3CF58895-866D-400A-B0E0-4CA98BF143F7/Library/Application%20Support/<my.app.id>/LocalContents/www/v2/indexConteneur.html

在模拟器上:file:///Users//Library/Developer/CoreSimulator/Devices/4A5B9713-6589-4DCA-ABBD-F4FF1246AE43/data/Containers/Data/Application/717B54FD-1344-41D3-8B53-FC0767FFE892/Library/Application%20Support//LocalContents/www/v2/indexConteneur.html

此代码在我尝试过的任何模拟器上都可以正常工作(ios 12/ios 13).

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

但是它不会在设备上加载 HTML 文件.当我使用 Safari 调试器时,我看到 webview 位于about:blank".

It however does not load the HTML file on the device. When I use the Safari debugger, I see that the webview is on "about:blank".

我在运行时没有错误,它似乎只是停止静默执行 loadFileURL.

I have no error at runtime, it just seem to stop executing the loadFileURL silently.

我可以做些什么来解决/调查这个问题?

What could I do to solve / investigate on this issue?

在设备上既没有调用 didFailNavigation 也没有调用 didFinishNavigation...

Neither the didFailNavigation nor the didFinishNavigation are called on device...

添加此代码:

NSFileManager *fileManager = [NSFileManager defaultManager];
NSLog(@"%@ exists? %d",URL.absoluteString,[fileManager fileExistsAtPath:URL.absoluteString]);

在设备上给出这个:2019-10-17 18:42:48.955588+0200 gall-app[881:230671]/var/mobile/Containers/Data/Application/3CF58895-866D-400A-B0E0-4CA98BF143F7/<my.app.id>/LocalContents/www/v2/indexConteneur.html 存在吗?1

所以它存在于预期的位置.我还使用 XCode 从设备下载了应用程序容器,我看到了它应该在的位置.

So it exists at the expected location. I've also downloaded the app container from the device with XCode and I see the file where it should be.

经过进一步测试,似乎 [self.webView loadFileURL:... 不适用于 ios 12(包括模拟器),但适用于 ios 13...

After further testing, it seems that [self.webView loadFileURL:... is not working on ios 12 (simulator included) but works on ios 13...

它们之间可能存在一些安全规则差异?

There might be some security rule difference between them?

在我的 info.plist 中,我有:

In my info.plist, I have:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
    <key>NSAllowsArbitraryLoadsInWebContent</key>
    <true/>
</dict>

我的 webview 是用这个代码初始化的:

And my webview is initialized with this code:

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.webView.UIDelegate = self;
    self.webView.navigationDelegate = self;
    
    self.webView.configuration.preferences.javaScriptEnabled = YES;
    self.webView.configuration.preferences.javaScriptCanOpenWindowsAutomatically = YES;
    
    [self.webView.configuration.preferences setValue:@YES forKey:@"allowFileAccessFromFileURLs"];
    [self.webView.configuration setValue:@YES forKey:@"allowUniversalAccessFromFileURLs"];
    
    [self addUserScriptToUserContentController:self.webView.configuration.userContentController];
    

推荐答案

好的,我找到了为什么"它不起作用的原因.我改变了这个:

OK I have found "why" it was not working. I changed this:

[self.webView loadFileURL:URL allowingReadAccessToURL:URL];

为此:

[self.webView loadFileURL:URL.absoluteURL allowingReadAccessToURL:rootAppURL];

rootAppURL 是:

NSString *rootappPath = [NSFileUtility pathRelativeToContentDirectoryForSubpath:@"/www"];
NSURL *rootAppURL = [NSURL fileURLWithPath:rootappPath isDirectory:YES];

它现在适用于 ios <13

And it now works on ios < 13

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

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