apache / cordova-ios

Apache Cordova iOS
https://cordova.apache.org/
Apache License 2.0
2.15k stars 986 forks source link

Load html file from data directory in cordova-ios from WKWebview #1011

Closed shamsundhar closed 9 months ago

shamsundhar commented 3 years ago

I am working on cordova-ios app. My App will download zip file(contains html files ex:-second_login.html) from server, then we are unzipping it in data directory. After successful unzipping, app should navigate to second_login.html. App is working fine in UIWebview with cordova-ios 5.1.1. But it is not working in cordova-ios-6.1.0 with WKWebview.

Using cordova-ios-6.1.0, I downloaded and unzipped in data-directory successfully to below path.

/var/mobile/Containers/Data/Application//Library/NoCloud///files/www-24862240/second_login.html.

And for loading this second_login.html, I am using document.location = ""

Initially, we got issue in downloading zip file(Cookie sync issue), for this we have used cordova-plugin-ios-xhr plugin. then we successfully downloaded zip file.

Please help me to resolve this issue. Thanks

AraHovakimyan commented 3 years ago

To load a local file you can use the following plugin. I hope this plugin can solve your problem.

cordova plugin add https://github.com/AraHovakimyan/cordova-plugin-wkwebviewxhrfix

NOTE: This plugin does not depend on any other additional plugins and does not require additional permissions.

shamsundhar commented 3 years ago

Already using https://github.com/TheMattRay/cordova-plugin-wkwebviewxhrfix plugin to access local files. But my problem here is i have one HTML file present in data directory. I need to load that HTML into cordova webkit webview. previously in UIWebview, we used document.location ="" to navigate to that HTML. please help

AraHovakimyan commented 3 years ago

Can you print in console your document.location by using consol.log(document.location)? You can try to use document.location ='www-24862240/second_login.htm';

afioletov commented 2 years ago

Good morning,

Did anybody solve the issue? We have the same problem - cannot change window.location to html file in cache. If we load file directly, document location is 'file:///private/var/containers/Bundle/Application/GUID/InMotionTestApp.app/www/Bootloader.html'.

Attempts to set location window.location='file:///var/mobile/Containers/Data/Application/GUID/Library/Caches/1.11.00000.0000/www/Default.html' fail with 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 I add schema and hostname to config.xml:

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

I 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

@AraHovakimyan I tried to use your plugin. It did not help.

snehasis commented 2 years ago

I am also facing the same issue where its unable to load the HTML file from the documents directory in WKWebview

snehasis commented 2 years ago

So I am using IONIC and able to solve this with below solution.

I used this as: window.location.href = window.Ionic.WebView.convertFileSrc(url);

My URL is in the format (for iOS): file:///var/mobile/Containers/Data/Application/[UUID]/Library/NoCloud/[appFolder]/index.html

May be in WkWebview try this window.WkWebView.convertFilePath(cordova.file.dataDirectory + path);

wulianpu commented 9 months ago

I have also encountered this issue. Does anyone have a solution?

breautek commented 9 months ago

WKWebView only allows to load within the "WKWebView" sandbox. This isn't necessary the same as the iOS sandbox, but by default it is.

When cordova-ios is loaded via WKURLScheme, the sandbox is the iOS bundle sandbox, because there is no API (afaik) that allows you to change the allowed directory via a loadRequest API.

When cordova-ios is in file-system mode, cordova uses loadFileURL:allowingReadAccessToURL which allowingReadAccessToURL must point to a non-empty directory that contains the main index.html

Cordova uses the directory if the index.html as the allowed read access directory. I think allowing any directory higher might be a security vulnerability, so I don't think this will change. However if you want to make the modification yourself, see: https://github.com/apache/cordova-ios/blob/a1f3acea0a59ca270769ff67ac7ff91fcae0d563/CordovaLib/Classes/Private/Plugins/CDVWebViewEngine/CDVWebViewEngine.m#L339C72-L339C95

Also note that any downloaded files must not use Cordova APIs to access native device APIs. Downloaded HTML files must work without modification and without any extensions in the standard Safari mobile browser. Accessing native device APIs from externally source code breaks Section 4.7 of Apple's policies.

For the 2 reasons listed above, I'm closing as won't fix.