-
Notifications
You must be signed in to change notification settings - Fork 760
fix(ios) issue #349 - navigate to cdvfile with wkwebview #358
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(ios) issue #349 - navigate to cdvfile with wkwebview #358
Conversation
…bview. WKWebView does not allow unknown url schemes so we have to intercept the request, stop it, and send out a new request for the equivalent file:// url. Note: When the lowest cordova supported iOS version is 11.0 this work-around can potentially be removed. WKWebview for iOS 11+ supports "WKURLSchemeHandler" and "setURLSchemeHandler" which should allow actual support for the cdvfile scheme.
8329648
to
901f22a
Compare
Hi, I am trying to use your fix for problems with cdvfile on ios. But I can't figure it out how this works. |
@tomaz-dugii Just to be clear, these changes are only relevant if you are trying to NAVIGATE to a If you are trying to display an image with a (If your situation is one of the above cases, I might be able to help.) If you are just having trouble figuring out what the correct url is for file you should look here: For your image in the documents folder, it should be something like: (personally, I just use the cordova.file.dataDirectory if you don't need to share image.png with the users) Otherwise I would recommend reading through the quirks section: |
Hi, thanks for reply. I did a workaround for this issue. |
I think this may break |
Since you would be on a file:// url, the location.reload() should just reload the file:// url which 99% should be fine. (But I am the OP, so of course I wouldn’t see any issues. :p) But, since cordova-ios has a minimum ios of 11 (I believe?), and with iOS 11+ there is WKURLSchemeHandler, which would probably be a better solution. |
To be clear, I didn't test (i dont even have mac hardware to test with). It was just speculation. I just have someone on slack who is on cordova-ios@6, using the schemes and is stating he has an issue using cdvfile urls on img nodes. It produces the same behavior as this pr is trying to fix... so it sounds like the same issue. |
Ah okay, interesting. I have only used the 2.x version of cordova-plugin-ionic-webview (which hosts a local server), so I am able go from https to the local server no problem. |
Since iOS 11+ is the minimum now this isn't necessary. If you enable the new scheme preference from cordova-ios@6.x.x you are able to go from <preference name="scheme" value="app" />
<preference name="hostname" value="localhost" /> Note:
|
@Lindsay-Needs-Sleep desperately need your help please :/ cordova-plugin-dialogs 2.0.2 "Notification" tried with both cdvfile:// scheme as well as the custom one you see above Code for loading the js:
(excuse the indentation) Any thoughts, ideas, prayers ? thanks |
@LonestarX91 The next thing is you should make sure you have your scheme <preference’s set correctly. (See my comment in this thread just above your post). If that doesn’t work, you can post your config.xml. (I use cordova-plugin-hostedwebapp to inject the cordova libraries onto our webpage. I was including them by a script tag at one point long ago, can’t remember if I ever fully got it working or not. I think it should be possible....) |
@Lindsay-Needs-Sleep thanks a lot for the quick input !
set the CSP to
and i still get
See config.xml below
I guess I'm gonna look into cordova-plugin-hostedwebapp for the moment, if you have any ideas why it's not working for me, please let me know Thanks LE: big oof...
|
@LonestarX91 Have you tired using this ionic plugin "cordova-plugin-ionic-webview". I've modified the url to match plugin recommendation. url = "ionic://localhost/cordova.js"; Can you give try once? |
changed everything in prefs (scheme), allow-intent, etc to ionic, changed CSP on the server to
changed my loading code to
aaaaand i get.... |
it seems that no matter what I try (tried ios 5.1.1 with xhr (+ fix) plugin etc) CSP always bites me in the ass... Refused to load cdvfile://localhost/bundle/www/cordova.js because it does not appear in the script-src directive of the Content Security Policy. Any more ideas please ? |
i'm beginning to think i am the only person in the world trying to load local cordova.js from remote, inside a wkwebview... |
Exposing I'm not sure what you're trying to accomplish but it sounds like you may be tackling your problem in the wrong way. |
@LonestarX91 It looks like you are doing everything right that I can think of. The error you received when trying to use it is easy to fix (it is just modifying 1 line in plugin.xml) You can see how to do it from any of the many forks of that plugin. (in github, you can go: insights > network to see the most recently active forks). Make sure you set up rules to only inject cordova onto your website to decrease the security vulnerability that breautek mentions. |
Platforms affected
iOS
Motivation and Context
Fixes #349
This issue is only present when using the wkwebview engine, but since that is the future of iOS this should be addressed. The wkWebview does not allow unknown schemes (eg.
cdvfile://
).Description
These changes intercept navigation requests and check if the url is a
cdvfile://
url. If it is, it stops the request and creates a new request for the equivalentfile://
url.Alternative solution:
Use a similar workaround to what is proposed by @guylando in pull request #296. This will get us past the unknown scheme, but I'm not sure how to get it to return the correct file from there. (Other than loading a request for the equivalent
file://
url).Future (better) solution:
When the lowest cordova supported iOS version is 11.0 this work-around can potentially be removed. WKWebview for iOS 11+ supports "WKURLSchemeHandler" and "setURLSchemeHandler" which should allow actual support for the cdvfile scheme.
Advice requested:
This is my first foray into objc and the ios side of cordova-plugin-file.
nativeUrlFromCdvfileUrl
is the most efficient way to get the equivalentfile://
url, please advise.window.location.href = 'cdvfile://...';
but thewindow.location.href
after the page load will actually befile://...
, can anyone see any issues that this could cause? (Especially issues that could be worse than not being able to navigate tocdvfile://
url at all?)Testing
Tested on ios 9.3.5 and 10.3.4 devices. Tested with and without wkwebview.
Ran
npm test
passed.Ran the automatic tests. No change from before and after the change. (One test failed before and after the additions (on andriod and ios), reported in issue #357)
Checklist
(platform)
if this change only applies to one platform (e.g.(android)
)