Skip to content
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

breaking: Use WKURLSchemeHandler for serving app content #781

Merged
merged 7 commits into from
Feb 27, 2020

Conversation

jcesarmobile
Copy link
Member

@jcesarmobile jcesarmobile commented Feb 11, 2020

breaking because serving from a custom scheme makes the apps lose existing data (localStorage, etc)

Used app as default scheme, but I'm open to using cordova, cdv or any other.
The scheme is configurable with "scheme" preference, the hostname is configurable with "hostname" preference.
App will be loaded from "app://localhost"
If the scheme is changed, it has to be allowed with an allow-navigation entry, app is allowed on the default template.

Note that at the moment, loading file urls (in example, camera image in a src tag) won't work because of cross domain requests not allowed. What we did on ionic webview plugin was to create an utility function that converts file:// urls to app://localhost/ urls, but not sure how to do it here. Put it in cordova.js?

closes: #415

@erisu
Copy link
Member

erisu commented Feb 11, 2020

I think cordova.js might be OK for this utility. Not sure of anywhere else it could go.

You mentioned that it was implemented in the ionic webview plugin. We have the cordova-ios/cordova-js-src/plugin/ios/wkwebkit.js file which was the content of the cordova-plugin-wkwebview-engine JS logic and is bundled in the final output of cordova.js.

Maybe it could be placed in that file?

Copy link
Member

@erisu erisu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code LGTM. 👍

I tested:

  • cordova platform add
  • cordova build ios
  • Tested with default config.xml
  • Tested with modified config.xml
<platform name="ios">
  <allow-navigation href="cdv://*" />
  <preference name="scheme" value="cdv" />
  <preference name="hostname" value="foobar" />
</platform>
  • Tested with CORS request remote
  • Tested with Fetch request remote
  • Tested with Fetch request local file

@erisu erisu added this to the 6.0.0 milestone Feb 12, 2020
@erisu
Copy link
Member

erisu commented Feb 13, 2020

You can rebase this PR with master and it should fix the failing tests now.

I just merged in #782 which fixed the failing tests with the latest node. This will now unblock the reviewing/merging hold up.

@erisu
Copy link
Member

erisu commented Feb 13, 2020

/me bot did not see request but pre-confirmed, rebase complete.

@@ -46,7 +46,7 @@ - (void)parser:(NSXMLParser*)parser didStartElement:(NSString*)elementName names
- (void)parserDidStartDocument:(NSXMLParser*)parser
{
// file: url <allow-navigations> are added by default
self.allowNavigations = [[NSMutableArray alloc] initWithArray:@[ @"file://" ]];
self.allowNavigations = [[NSMutableArray alloc] initWithArray:@[ @"file://", @"app://" ]];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this "app://" scheme fixed? (In this case, do we expect that the user add allow-navigation directive in config.xml in order to allow his own scheme?)

I think it is better to use [settings cordovaSettingForKey:@"scheme"]; here instead of fixed "app://", if possible.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can change the default by using the preference scheme option. But if they change, the must add allow-navigation

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, I thought to only allow the default one, but we can easily allow the configured one without an additional allow-navigation, will change it

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the user can enter an invalid scheme (i.e. http, https, etc) that will default to "app", instead of reading directly from settings, I've created an appScheme in the view controller and use that for allowing the navigation.

@dpogue
Copy link
Member

dpogue commented Feb 18, 2020

breaking because serving from a custom scheme makes the apps lose existing data (localStorage, etc)

Is there an option to continue using file:/// URLs for apps that can't risk the data loss? Speaking personally, losing existing data would be unacceptable for at least 2 of my apps. I've already had to deal with data migration once from UIWebView to WKWebView.

Copy link
Contributor

@knight9999 knight9999 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks good!

@jcesarmobile
Copy link
Member Author

breaking because serving from a custom scheme makes the apps lose existing data (localStorage, etc)

Is there an option to continue using file:/// URLs for apps that can't risk the data loss? Speaking personally, losing existing data would be unacceptable for at least 2 of my apps. I've already had to deal with data migration once from UIWebView to WKWebView.

It could be done, but I did it this way because in your proposal you said “Update to use Scheme Handlers with WKWebView.”, not “add an option to use Scheme Handlers” or something like that. Also we have dropped iOS 10 support because of that.
And data can still be migrated, it’s just not handled by cordova itself, it has to be the developer who does the migration.
So maybe we should discuss in your proposal or in the dev list if we should keep serving from file as an option.

@jcesarmobile
Copy link
Member Author

BTW, you should be able to install cordova-plugin-wkwebview-engine and that should make your app use file instead of a custom scheme.

@erisu
Copy link
Member

erisu commented Feb 25, 2020

@dpogue from your question/concern and @jcesarmobile response, is it OK to continue with merging this PR in?

Or do you have other questions?

Trying to get a status on this PR.

@jcesarmobile
Copy link
Member Author

Going to merge since there is no response

In case we decide to add the file:// back as an option, it can be done in a separate PR, or cordova-plugin-wkwebview-engine can still be used

@jcesarmobile jcesarmobile merged commit f09b879 into apache:master Feb 27, 2020
@jcesarmobile jcesarmobile deleted the scheme-handlers branch February 27, 2020 09:43
@brodycj
Copy link
Contributor

brodycj commented Feb 27, 2020

Thanks for your work on this!

@imselvakumar
Copy link

Hello,

Able to retrieve app contents from remote http web page. But not able to retrieve app contents from remote https web page. Can you please help me on this.

@NiklasMerz
Copy link
Member

Please go to Stack Overflow or Slack for questions.

Issues and pullrequests are not for questions.

@imselvakumar
Copy link

@NiklasMerz okay sure. Thanks!

@breautek breautek mentioned this pull request Jun 26, 2020
1 task
@lovelyelfpop
Copy link

How to display local images in application storage directory (e.g. file:///var/mobile/Applications//) after add <preference name="scheme" value="cdv" /> <preference name="hostname" value="localhost" /> ?

@lovelyelfpop
Copy link

Got a solution, #947

@Pratz007-tech
Copy link

Hi with cordovaios@6.1.0 I have set preferences with scheme: value="app" and hostname: value="localhost" because of which my origin in api calls is going as app:localhost resulting in cors issue i tried scheme: value="http" and hostname value="myDomain" expecting origin in api call as http:myDomain . But this is not working. Any suggestions would be appreciated :) Thanks !!

@Pratz007-tech
Copy link

Code LGTM. 👍

I tested:

  • cordova platform add
  • cordova build ios
  • Tested with default config.xml
  • Tested with modified config.xml
<platform name="ios">
  <allow-navigation href="cdv://*" />
  <preference name="scheme" value="cdv" />
  <preference name="hostname" value="foobar" />
</platform>
  • Tested with CORS request remote
  • Tested with Fetch request remote
  • Tested with Fetch request local file

Can I configure scheme & hostname like scheme as http & hostname as myDomain ? I tried but it still showed app:myDomain in origin while doing api calls.

@timbru31
Copy link
Member

Please seek support via asking on StackOverflow or in our Slack community.

@erisu
Copy link
Member

erisu commented Aug 25, 2020

No, there are reserved schemes that can not be used for example http, https, and file. If you set a scheme that is invalid (reserved), it will default to app.

This information had also been written already in this PR. Please read the comments as well before asking.

@apache apache locked as resolved and limited conversation to collaborators Aug 25, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support serving local content with WKURLSchemeHandler
10 participants