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

Conversation

@YukiOya
Copy link
Contributor

@YukiOya YukiOya commented Dec 7, 2018

It possible to get and set custom user agent to webview.

if (error) {
result([FlutterError
errorWithCode:@"getUserAgent_failed"
message:@"Failed gettting UserAgent"
Copy link
Contributor

Choose a reason for hiding this comment

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

typo

Copy link
Contributor Author

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}];
Copy link
Contributor

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?

Copy link
Contributor

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.

Copy link
Contributor

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.

Copy link
Contributor

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).

Copy link
Contributor Author

@YukiOya YukiOya Dec 10, 2018

Choose a reason for hiding this comment

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

Sorry for forgetting that.

I implemented it referencing SO, and added the source code comment. 89e010b

if (@available(iOS 9.0, *)) {
[_webView setCustomUserAgent:userAgent];
} else if (userAgent) {
[[NSUserDefaults standardUserDefaults] registerDefaults:@{@"UserAgent" : userAgent}];
Copy link
Contributor

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];
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fix it.
360e733

@YukiOya
Copy link
Contributor Author

YukiOya commented Dec 21, 2018

@amirh Thanks for the review.
I fixed the issues, and added new commit to fix the accessor name like currentUrl.

@msdk27
Copy link

msdk27 commented Jan 10, 2019

Hey, thanks for the missing part in the webview plugin... my question is, how to use the own userAgent?! Is there a documentation?

@YukiOya
Copy link
Contributor Author

YukiOya commented Jan 11, 2019

@msdk27 Hi. Thank you for your question.
Now, the custom userAgent apply to webview only if it initialize with constructor argument like this.

WebView(userAgent: "custom useragent");

Perhaps, the interface will change by maintainer's reviews.

@msdk27
Copy link

msdk27 commented Jan 11, 2019

@msdk27 Hi. Thank you for your question.
Now, the custom userAgent apply to webview only if it initialize with constructor argument like this.

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.

@cyanglaz cyanglaz changed the title Support UserAgent setting for webview_flutter [webview_flutter]Support UserAgent setting for webview_flutter Feb 22, 2019
@amirh
Copy link
Contributor

amirh commented Mar 29, 2019

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).

Copy link
Contributor

@amirh amirh left a 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.

@YukiOya
Copy link
Contributor Author

YukiOya commented Apr 1, 2019

Hi, @amirh
Thank you for reply.
This PR is rebased, and added e2e tests.

Copy link
Contributor

@amirh amirh left a 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.
Copy link
Contributor

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:

  1. What would be the user agent when this is null?
  2. What happens when the widget is rebuild with a new userAgent? is it sending a new request with the new user agent?
  3. What happens when goBack is 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 {
Copy link
Contributor

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.
Copy link
Contributor

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');
});
}
Copy link
Contributor

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.

@timrijckaert
Copy link

What needs to be done to get this merged?

@aegis123
Copy link

aegis123 commented Jul 5, 2019

@YukiOya any change you can get the last remarks fixed so this can be merged?

@amirh
Copy link
Contributor

amirh commented Jul 8, 2019

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.
Note that the priority of the feature(and of reviewing non trivial PRs) is determined by the number thumbs up on the issue(flutter/flutter#28256 in this case). See the review process section in the contributing guide.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants