Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

[webview_flutter] Add interface methods to load local files and HTML strings. #4446

Merged
merged 8 commits into from
Nov 4, 2021

Conversation

mvanbeusekom
Copy link
Contributor

@mvanbeusekom mvanbeusekom commented Oct 22, 2021

Adds methods to the webview_flutter_platform_interface package to support loading content from local files and HTML string directly.

Related to issues:

If you had to change anything in the flutter/tests repo, include a link to the migration guide as per the breaking change policy.

Pre-launch Checklist

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • I read the Tree Hygiene wiki page, which explains my responsibilities.
  • I read and followed the relevant style guides and ran the auto-formatter. (Note that unlike the flutter/flutter repo, the flutter/plugins repo does use dart format.)
  • I signed the CLA.
  • The title of the PR starts with the name of the plugin surrounded by square brackets, e.g. [shared_preferences]
  • I listed at least one issue that this PR fixes in the description above.
  • I updated pubspec.yaml with an appropriate new version according to the pub versioning philosophy.
  • I updated CHANGELOG.md to add a description of the change.
  • I updated/added relevant documentation (doc comments with ///).
  • I added new tests to check the change I am making or feature I am adding, or Hixie said the PR is test exempt.
  • All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel on Discord.

Adds methods to the webview_flutter_platform_interface to support
loading content from Flutter asset defined in the pubspec.yaml of
directly from HTML string.
@google-cla google-cla bot added the cla: yes label Oct 22, 2021
@github-actions github-actions bot added the p: webview_flutter Edits files for a webview_flutter plugin label Oct 22, 2021
Future<void> loadFlutterAsset(String assetName) async {
assert(assetName != null);
return _channel.invokeMethod<void>('loadFlutterAsset', assetName);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

You've listed issues for both assets and arbitrary local files in the PR description; why are we restricting to assets here?

It's not clear to me that we should actually support assets on a per-plugin basis, in fact, since there are other cases where someone may want asset paths (e.g., FFI). It might make more sense to just support file paths, and then have a dedicated plugin (or maybe a function in path_provider) to get asset paths.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As far as I know, it is not possible to load files in the WKWebView control that live outside the application bundle (due to security reasons). By only allowing loading of Flutter assets you force developers to add their HTML files as part of the iOS bundle ensuring that the file is always available and can be loaded.

This might be a bit restrictive to other platforms however I think it will cover most use cases where people wish to load local files. At least that was my reasoning.

Copy link
Contributor

Choose a reason for hiding this comment

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

As far as I know, it is not possible to load files in the WKWebView control that live outside the application bundle (due to security reasons).

Do you have a source for that? It's not a documented restriction of the file-loading API.

This might be a bit restrictive to other platforms however I think it will cover most use cases where people wish to load local files. At least that was my reasoning.

One of the issues you linked is explicitly about a use case where assets wouldn't work; I don't think we have any data on how common one or the other use case is.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No I don't have any documentation to back that up. And there are indeed samples that allow you to load information from for example the Documents directory. My bad.

I will update the interface to accept an absolute file path instead.

Copy link
Contributor Author

@mvanbeusekom mvanbeusekom Oct 27, 2021

Choose a reason for hiding this comment

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

Done. I those to accept only the absolute file path as that seems to be the easiest way to work cross platform.

On Android the WebView.loadUrl method requires the path to be prefixed with file:// (which we can take care of internally).

The [WKWebView loadFileURL:] only accepts an instance of the NSURL class which can easily be created from the absolute path using the [NSURL fileURLWithPath:] method.

}

@override
Future<void> loadHtml(
Copy link
Contributor

Choose a reason for hiding this comment

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

How about loadHtmlString; we expect all of the mechanism for loading to provide HTML so this seems confusingly vague.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Renamed.

Copy link
Contributor

@stuartmorgan stuartmorgan left a comment

Choose a reason for hiding this comment

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

LGTM

@mvanbeusekom mvanbeusekom changed the title [webview_flutter] Add interface methods to load HTML and Flutter assets. [webview_flutter] Add interface methods to load local files and HTML strings. Oct 27, 2021
@mvanbeusekom mvanbeusekom added the waiting for tree to go green (Use "autosubmit") This PR is approved and tested, but waiting for the tree to be green to land. label Oct 27, 2021
@fluttergithubbot
Copy link

This pull request is not suitable for automatic merging in its current state.

  • The status or check suite android-platform_tests has failed. Please fix the issues identified (or deflake) before re-applying this label.

@fluttergithubbot fluttergithubbot removed the waiting for tree to go green (Use "autosubmit") This PR is approved and tested, but waiting for the tree to be green to land. label Oct 27, 2021
@mvanbeusekom mvanbeusekom added the waiting for tree to go green (Use "autosubmit") This PR is approved and tested, but waiting for the tree to be green to land. label Nov 4, 2021
@mvanbeusekom mvanbeusekom merged commit e39a584 into flutter:master Nov 4, 2021
@mvanbeusekom mvanbeusekom deleted the feature/webview_load_asset branch November 4, 2021 15:04
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Nov 4, 2021
NickalasB added a commit to NickalasB/plugins that referenced this pull request Nov 4, 2021
* master:
  [webview_flutter] Deprecate evaluateJavascript in favour of runJavaScript and runJavaScriptForResult. (flutter#4403)
  [webview_flutter] Add interface methods to load local files and HTML strings. (flutter#4446)
  [ci] add pedantic dep to new in_app_purchase pkgs (flutter#4471)
  [ci] Remove unused dep from file_selector. (flutter#4468)
  [ci] update build_runner dep in android_intent and file_selector example (flutter#4467)
  [webview_flutter] Add platform interface method `loadRequest`. (flutter#4450)
  Remove -Werror from deprecated plugin Android builds (flutter#4466)
  [webview_flutter] Update webview packages for Android and iOS to implement `runJavascript` and `runJavascriptReturningResult`. (flutter#4402)
  [camera] Fix CamcorderProfile Usages (flutter#4423)
  [google_sign_in] Use a transparent image as a placeholder for the `GoogleUserCircleAvatar` (flutter#4391)
  [in_app_purchase]IAP/platform interface add cancel status (flutter#4093)
  [image_picker] doc:readme image picker misprints (flutter#4421)
  Support Hybrid Composition through the GoogleMaps Widget (flutter#4082)
  [path_provider] started supporting background platform channels (flutter#4443)
  [device_info] started using Background Platform Channels (flutter#4456)

# Conflicts:
#	packages/webview_flutter/webview_flutter/CHANGELOG.md
#	packages/webview_flutter/webview_flutter/pubspec.yaml
amantoux pushed a commit to amantoux/plugins that referenced this pull request Dec 11, 2021
…strings. (flutter#4446)

* Add methods to load HTML and Flutter assets.

Adds methods to the webview_flutter_platform_interface to support
loading content from Flutter asset defined in the pubspec.yaml of
directly from HTML string.

* Renamed loadHtml to loadHtmlString

* Support loading arbitrary files instead of only Flutter assets

* Update changelog description

* Fix formatting

* Fix formatting
KyleFin pushed a commit to KyleFin/plugins that referenced this pull request Dec 21, 2021
…strings. (flutter#4446)

* Add methods to load HTML and Flutter assets.

Adds methods to the webview_flutter_platform_interface to support
loading content from Flutter asset defined in the pubspec.yaml of
directly from HTML string.

* Renamed loadHtml to loadHtmlString

* Support loading arbitrary files instead of only Flutter assets

* Update changelog description

* Fix formatting

* Fix formatting
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
cla: yes p: webview_flutter Edits files for a webview_flutter plugin waiting for tree to go green (Use "autosubmit") This PR is approved and tested, but waiting for the tree to be green to land.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants