-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[webview_flutter]Support UserAgent setting for webview_flutter #968
Conversation
| if (error) { | ||
| result([FlutterError | ||
| errorWithCode:@"getUserAgent_failed" | ||
| message:@"Failed gettting UserAgent" |
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.
typo
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.
Fix it. cdc320c
| if (@available(iOS 9.0, *)) { | ||
| [_webView setCustomUserAgent:userAgent]; | ||
| } else if (userAgent) { | ||
| [[NSUserDefaults standardUserDefaults] registerDefaults:@{@"UserAgent" : userAgent}]; |
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.
Why is this required? Where is the value read again?
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.
Looks like WkWebView reads that value on construction, I did not find official documentation for this other than SO, but I did verify that it works.
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.
In that case.. I’d recommend a short source code comment, perhaps a link to the relevant SO answer.
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.
+1 we should definitely mention that in a comment if we end up using this trick(though I wouldn't link SO).
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.
| if (@available(iOS 9.0, *)) { | ||
| [_webView setCustomUserAgent:userAgent]; | ||
| } else if (userAgent) { | ||
| [[NSUserDefaults standardUserDefaults] registerDefaults:@{@"UserAgent" : userAgent}]; |
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.
Looks like WkWebView reads that value on construction, I did not find official documentation for this other than SO, but I did verify that it works.
| } else if (userAgent) { | ||
| [[NSUserDefaults standardUserDefaults] registerDefaults:@{@"UserAgent" : userAgent}]; | ||
| WKWebViewConfiguration* configuration = _webView.configuration; | ||
| _webView = [[WKWebView alloc] initWithFrame:_webView.frame configuration:configuration]; |
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 can't do this as it won't change the webview reference that the platform views controller is holding.
Even if we could update the engine's reference, doing this will lose any state the webview is currently in.
I think a better approach for iOS < 9 would be to do the user defaults trick prior to the construction of the webview in FLTWebViewController's initWithFrame (ohh I just noticed we have a typo there).
And just not support changing the userAgent for iOS < 9 (as WkWebView doesn't support it).
Note that we should make sure to restore the UserAgent value after constructing the webview.
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.
Fix it.
360e733
|
@amirh Thanks for the review. |
|
Hey, thanks for the missing part in the webview plugin... my question is, how to use the own userAgent?! Is there a documentation? |
|
@msdk27 Hi. Thank you for your question. WebView(userAgent: "custom useragent");Perhaps, the interface will change by maintainer's reviews. |
Hi thank you for response. Do you have an example? it does not work for me. |
|
Sorry for the delay getting to review this, we were trying to minimize adding new features before we had e2e tests. The code has changed a bit since this PR was sent, this PR needs to be rebased, and add e2e tests should be added(see https://github.com/flutter/plugins/blob/master/packages/webview_flutter/example/test_driver/webview.dart). |
amirh
left a comment
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.
Please rebase, and add e2e tests.
|
Hi, @amirh |
amirh
left a comment
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.
Thanks for the update, left a few comments.
| /// [WebViewController.evaluateJavascript] can assume this. | ||
| final PageFinishedCallback onPageFinished; | ||
|
|
||
| /// The custom UserAgent. |
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 should have more information in this Dartdoc, some question I think I might be having if I were looking here:
- What would be the user agent when this is null?
- What happens when the widget is rebuild with a new userAgent? is it sending a new request with the new user agent?
- What happens when
goBackis called after a user agent was changed? is the new or previous user agent used?
In general I recommend reading the documentation section in the Flutter style guide which we should follow here.
| } | ||
|
|
||
| /// Accessor the UserAgent. | ||
| Future<String> userAgent() async { |
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: getUserAgent
| return _channel.invokeMethod("reload"); | ||
| } | ||
|
|
||
| /// Accessor the UserAgent. |
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.
Please follow the documentation section in the Flutter style guide, I'd suggest:
/// Returns the User-Agent value that will be used for subsequent HTTP requests.
| final String userAgent = await controller.userAgent(); | ||
| expect(userAgent, 'UA'); | ||
| }); | ||
| } |
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.
Please add a test that rebuilds a WebView with a different user agent.
|
What needs to be done to get this merged? |
|
@YukiOya any change you can get the last remarks fixed so this can be merged? |
|
This PR needs more work before it can be merged, a rebase/merge of master is needed(a recent refactoring that was done requires moving some stuff around, and following up on the review comments is still needed). I'm marking this PR as "needs love" and closing it, feel free to re-open or file a new PR. |
It possible to get and set custom user agent to webview.