Skip to content

Commit c7008cf

Browse files
[webview_flutter_wkwebview] Fixes bug where WebkitWebViewController.getUserAgent was incorrectly returning an empty String (flutter#5062)
It seems that [WKWebView.customUserAgent](https://developer.apple.com/documentation/webkit/wkwebview/1414950-customuseragent?language=objc) is never actually set to nil. It was always an empty String. Even after attempting to set the value to nil, the value would always return as an empty String. This is in contrary to the documentation for the property. I'm not sure if this is based on the iOS version, so this PR checks if the value is null or is empty. Fixes flutter#135813
1 parent cfe0c21 commit c7008cf

File tree

4 files changed

+10
-2
lines changed

4 files changed

+10
-2
lines changed

packages/webview_flutter/webview_flutter_wkwebview/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 3.9.1
2+
3+
* Fixes bug where `WebkitWebViewController.getUserAgent` was incorrectly returning an empty String.
4+
15
## 3.9.0
26

37
* Adds support for `PlatformWebViewController.getUserAgent`.

packages/webview_flutter/webview_flutter_wkwebview/example/integration_test/webview_flutter_test.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,7 @@ Future<void> main() async {
313313

314314
final String? userAgent = await controller.getUserAgent();
315315
expect(userAgent, isNotNull);
316+
expect(userAgent, isNotEmpty);
316317
});
317318

318319
group('Video playback policy', () {

packages/webview_flutter/webview_flutter_wkwebview/lib/src/webkit_webview_controller.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,10 @@ window.addEventListener("error", function(e) {
666666
@override
667667
Future<String?> getUserAgent() async {
668668
final String? customUserAgent = await _webView.getCustomUserAgent();
669-
if (customUserAgent != null) {
669+
// Despite the official documentation of `WKWebView.customUserAgent`, the
670+
// default value seems to be an empty String and not null. It's possible it
671+
// could depend on the iOS version, so this checks for both.
672+
if (customUserAgent != null && customUserAgent.isNotEmpty) {
670673
return customUserAgent;
671674
}
672675

packages/webview_flutter/webview_flutter_wkwebview/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: webview_flutter_wkwebview
22
description: A Flutter plugin that provides a WebView widget based on Apple's WKWebView control.
33
repository: https://github.com/flutter/packages/tree/main/packages/webview_flutter/webview_flutter_wkwebview
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+webview%22
5-
version: 3.9.0
5+
version: 3.9.1
66

77
environment:
88
sdk: ">=2.19.0 <4.0.0"

0 commit comments

Comments
 (0)