cordova-ios icon indicating copy to clipboard operation
cordova-ios copied to clipboard

WkWebView cannot open any pages from data container

Open afioletov opened this issue 3 years ago • 3 comments

Bug Report

Problem

WkWebView cannot open any pages from data container. There are no issues with pages in bundle. Application can download and save new pages/resources into cache folder (cordova.file.cacheDirectory), but attempts to set windows.location to some page in cache folder fail.

What is expected to happen?

It worked fine with UIWebView, and it would be great to have the same possibility with WkWebView

What does actually happen?

If we load file directly: <content src="Bootloader.html" /> then document location is set to:

'file:///private/var/containers/Bundle/Application/GUID/InMotionTestApp.app/www/Bootloader.html'.

We check if cached version exists and try to set location: window.location='file:///var/mobile/Containers/Data/Application/GUID/Library/Caches/1.11.00000.0000/www/Default.html'

Such attempts fail with 'outside sandbox' error, while we try to open files from sandbox:

2021-07-30 10:06:33.742306-0400 InMotionTestApp[2318:560700] [Loading] Received an unexpected URL from the web process
2021-07-30 10:06:33.742441-0400 InMotionTestApp[2318:560700] [Process] 0x10202ca18 - [pageProxyID=11, webPageID=12, PID=2321] WebPageProxy::Ignoring request to load this main resource because it is outside the sandbox


If we add schema and hostname to config.xml like it's recommended in blog:

<content src="app://localhost/Bootloader.html" />
...
 <preference name="scheme" value="app" />
 <preference name="hostname" value="localhost" />

We do not get error in Xcode, but get error in web inspector:

Not allowed to load local resource: file:///var/mobile/Containers/Data/Application/A3E5D945-5503-4D23-A54F-C1135CE08CD8/Library/Caches/1.11.00000.0000/www/Default.html

Replacing 'file://' with 'app://localhost' does not help, it just generates 404 error.

Version information

Cordova CLI 10.0.0 "cordova-ios": "^6.2.0", "cordova-custom-config": "^5.1.0", "cordova-plugin-auth-dialog": "^0.1.6", "cordova-plugin-camera": "^4.1.0", "cordova-plugin-compat": "^1.2.0", "cordova-plugin-device": "^2.0.3", "cordova-plugin-emm-app-config": "^1.0.2", "cordova-plugin-file": "^6.0.2", "cordova-plugin-inappbrowser": "^5.0.0", "cordova-plugin-network-information": "^2.0.2", "cordova-plugin-statusbar": "^2.4.3", "cordova-plugin-whitelist": "^1.3.4", "cordova-plugin-zeep": "0.0.4"

I debug it on iOS 14.6 from Xcode 12.5.1 on macOS Big Sur 11.5

Checklist

  • [X] I searched for existing GitHub issues
  • [X] I updated all Cordova tooling to most recent version
  • [X] I included all the necessary information above

afioletov avatar Aug 03 '21 14:08 afioletov

I created simple swift application that does the same: load file from bundle and tries to change location to data folder (cache). It fails the same way.

Actually WebKit can load files from data container as well. I changed loadRequest method and load file from cache if it exists.


            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
            NSString *cacheDirectory = [paths objectAtIndex:0];
            
            NSURL *nsurl = [NSURL fileURLWithPath:[[[cacheDirectory stringByAppendingPathComponent:@"1.11.00000.0000"] stringByAppendingPathComponent:@"www"] stringByAppendingPathComponent:@"Bootloader.html"]]; //locally
            NSURL *readAccessToURL1 = [NSURL fileURLWithPath:@"/"];
            if(nsurl)
                return [(WKWebView*)_engineWebView loadFileURL:nsurl allowingReadAccessToURL:readAccessToURL1];


It works fine, but it cannot load scripts from bundle. Looks like WebKit fails to crossing borders between containers within sandbox. Is there any configuration settings to allow it?

I created a bug in webkit's bugzilla.

afioletov avatar Aug 11 '21 11:08 afioletov

In our case https://github.com/apache/cordova-ios/issues/1281 changing the page via window.location.href = "file:///..." simply does not work anymore.

sombatos avatar Jan 03 '23 13:01 sombatos

... localFile = window.WKWebView.convertFilePath(localFile) window.open(localFile)

works for me, Cordova iOS 6.2.0

remoorejr avatar Jan 03 '23 16:01 remoorejr