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

[webview_flutter] Implementations of loadFile and loadHtmlString for WKWebView #4486

Merged
merged 8 commits into from
Nov 12, 2021

Conversation

mvanbeusekom
Copy link
Contributor

@mvanbeusekom mvanbeusekom commented Nov 8, 2021

Adds implementations of the loadFile and loadHtmlString method channel calls for the webview_flutter_wkwebview package.

The loadFile and loadHtmlString methods have been added to the platform interface by PR #4446 and this PR will add the WKWebView implementation.

The two important commits are:

  • c1dc83a: contains the implementation, tests and example usage for the loadFile method channel call;
  • f1343c2: contains the implementation, tests and example usage for the loadHtmlString method channel call.

Related to issues:

NOTE: as per offline discussion with @stuartmorgan the loading of Flutter assets (assets registered within the pubspec.yaml) should be handled via the loadFile method and retrieve the path to the physical asset location the path_provider should be used (or something similar). At the moment the path_provider doesn't support returning the path to the physical location of Flutter assets there is however an issue (flutter/flutter#34026) to track this functionality.

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 native implementation for the `loadFile` method channel call to the
webview_flutter_wkwebview package.
Adds native implementation for the `loadHtmlString` method channel call to the
webview_flutter_wkwebview package.
@google-cla google-cla bot added the cla: yes label Nov 8, 2021
@github-actions github-actions bot added p: webview_flutter Edits files for a webview_flutter plugin platform-ios labels Nov 8, 2021
Copy link
Member

@BeMacized BeMacized left a comment

Choose a reason for hiding this comment

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

LGTM

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.

Minor nits, but looks good otherwise.

<body>

<h1>Local demo page</h1>
<p>This is an example page used to demonstrate how to load a local file or HTML string using the <a href="https://pub.dev/packages/webview_flutter">Flutter webview</a> plugin.</p>
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: There's no reason not to break this line to wrap to 80 characters, since HTML doesn't care about whitespace.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

File indexFile = File('$tmpDir/www/index.html');

if (await indexFile.exists()) {
return indexFile.path;
Copy link
Contributor

Choose a reason for hiding this comment

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

This early return seems odd; why would we want not changes someone made locally to the example HTML (e.g., to try to reproduce an issue in the context of the example) after having already run once to take effect?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You are right. I added this check with the idea that if the file exists we don't have to write it again (in case the user presses the button twice in the same session). However I didn't think about the fact that the file will be persisted over multiple sessions and developers might want to change the HTML contents.

Future<void> loadFile(
String absoluteFilePath,
) {
assert(absoluteFilePath != null || absoluteFilePath.isNotEmpty);
Copy link
Contributor

Choose a reason for hiding this comment

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

We don't need null assertions in example code that's null-safe; no non-null-safe code can call it since it's not a library.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed the redundant assert.

String html, {
String? baseUrl,
}) {
assert(html != null || html.isNotEmpty);
Copy link
Contributor

Choose a reason for hiding this comment

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

Same.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed the redundant assert.

return;
}

NSURL* url = [NSURL fileURLWithPath:[call arguments] isDirectory:NO];
Copy link
Contributor

Choose a reason for hiding this comment

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

You should validate that this is non-nil before passing it below (returning an error if it is nil) in case someone passes a garbage string that doesn't parse as a path.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added additional check to ensure the url variable is not nil, return an error if the variable is nil.


- (void)onLoadHtmlString:(FlutterMethodCall*)call result:(FlutterResult)result {
NSDictionary* arguments = [call arguments];
if (!arguments || ![arguments isKindOfClass:NSDictionary.class]) {
Copy link
Contributor

Choose a reason for hiding this comment

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

The first check is redundant; nil isKindOfClass: will always return false.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed the redundant check.

@@ -517,6 +564,29 @@ - (void)updateUserAgent:(NSString*)userAgent {
}
}

+ (bool)isValidStringArgument:(id)argument withErrorMessage:(NSString**)errorDetails {
Copy link
Contributor

Choose a reason for hiding this comment

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

Needs a declaration comment, per style guide.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

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 with final nits

@@ -315,7 +315,6 @@ class WebViewController {
Future<void> loadFile(
String absoluteFilePath,
) {
assert(absoluteFilePath != null || absoluteFilePath.isNotEmpty);
Copy link
Contributor

Choose a reason for hiding this comment

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

Don't you still want the non-emtpy assert here and below?

* @param argument The argument that should be validated.
* @param errorDetails An optional NSString variable which will contain a detailed error message in
* case the supplied argument is not valid.
* @return `true` if the given `argument` is a valid non-null, non-empty string; otherwise `false`.
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: "YES" and "NO", not "true" and "false"

* @param errorDetails An optional NSString variable which will contain a detailed error message in
* case the supplied argument is not valid.
* @return `true` if the given `argument` is a valid non-null, non-empty string; otherwise `false`.
*/
+ (bool)isValidStringArgument:(id)argument withErrorMessage:(NSString**)errorDetails {
Copy link
Contributor

Choose a reason for hiding this comment

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

Oops, and I missed that this is bool; it should be BOOL.

@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 12, 2021
@fluttergithubbot fluttergithubbot merged commit 66ed8f4 into flutter:master Nov 12, 2021
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Nov 12, 2021
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Nov 12, 2021
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Nov 12, 2021
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Nov 12, 2021
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 platform-ios 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
4 participants