-
Notifications
You must be signed in to change notification settings - Fork 9.8k
[webview_flutter] Implementations of loadFile
and loadHtmlString
for WKWebView
#4486
[webview_flutter] Implementations of loadFile
and loadHtmlString
for WKWebView
#4486
Conversation
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.
packages/webview_flutter/webview_flutter_wkwebview/example/lib/main.dart
Outdated
Show resolved
Hide resolved
packages/webview_flutter/webview_flutter_wkwebview/example/lib/main.dart
Show resolved
Hide resolved
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
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.
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> |
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.
Nit: There's no reason not to break this line to wrap to 80 characters, since HTML doesn't care about whitespace.
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.
Fixed.
File indexFile = File('$tmpDir/www/index.html'); | ||
|
||
if (await indexFile.exists()) { | ||
return indexFile.path; |
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.
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?
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 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); |
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.
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.
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.
Removed the redundant assert.
String html, { | ||
String? baseUrl, | ||
}) { | ||
assert(html != null || html.isNotEmpty); |
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.
Same.
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.
Removed the redundant assert.
return; | ||
} | ||
|
||
NSURL* url = [NSURL fileURLWithPath:[call arguments] isDirectory:NO]; |
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 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.
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.
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]) { |
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.
The first check is redundant; nil isKindOfClass:
will always return false.
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.
Removed the redundant check.
@@ -517,6 +564,29 @@ - (void)updateUserAgent:(NSString*)userAgent { | |||
} | |||
} | |||
|
|||
+ (bool)isValidStringArgument:(id)argument withErrorMessage:(NSString**)errorDetails { |
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.
Needs a declaration comment, per style guide.
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.
f78a3bc
to
d5cf9f9
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 with final nits
@@ -315,7 +315,6 @@ class WebViewController { | |||
Future<void> loadFile( | |||
String absoluteFilePath, | |||
) { | |||
assert(absoluteFilePath != null || absoluteFilePath.isNotEmpty); |
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.
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`. |
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.
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 { |
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.
Oops, and I missed that this is bool
; it should be BOOL
.
…String` for WKWebView (flutter/plugins#4486)
…String` for WKWebView (flutter/plugins#4486)
…String` for WKWebView (flutter/plugins#4486)
…String` for WKWebView (flutter/plugins#4486)
Adds implementations of the
loadFile
andloadHtmlString
method channel calls for the webview_flutter_wkwebview package.The
loadFile
andloadHtmlString
methods have been added to the platform interface by PR #4446 and this PR will add the WKWebView implementation.The two important commits are:
loadFile
method channel call;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
dart format
.)[shared_preferences]
///
).If you need help, consider asking for advice on the #hackers-new channel on Discord.