-
Notifications
You must be signed in to change notification settings - Fork 9.8k
[webview_flutter] Add interface methods to load local files and HTML strings. #4446
[webview_flutter] Add interface methods to load local files and HTML strings. #4446
Conversation
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.
Future<void> loadFlutterAsset(String assetName) async { | ||
assert(assetName != null); | ||
return _channel.invokeMethod<void>('loadFlutterAsset', assetName); | ||
} |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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( |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Renamed.
9b08218
to
0f28905
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
packages/webview_flutter/webview_flutter_platform_interface/CHANGELOG.md
Outdated
Show resolved
Hide resolved
This pull request is not suitable for automatic merging in its current state.
|
38c0578
to
d905be9
Compare
d905be9
to
0f70b11
Compare
* 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
…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
…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
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
dart format
.)[shared_preferences]
///
).If you need help, consider asking for advice on the #hackers-new channel on Discord.