From 67919417c46c966a5f59143fa5fedd55e8f2b4cf Mon Sep 17 00:00:00 2001 From: Stuart Morgan Date: Fri, 3 Oct 2025 11:49:18 -0400 Subject: [PATCH] [webview_flutter] Add Android geolocation README Adds a section to `webview_flutter_android`'s README explaining how to enable geolocation. Fixes https://github.com/flutter/flutter/issues/173279 --- .../webview_flutter_android/CHANGELOG.md | 4 ++++ .../webview_flutter_android/README.md | 23 +++++++++++++++++++ .../example/lib/readme_excerpts.dart | 19 +++++++++++++++ .../webview_flutter_android/pubspec.yaml | 2 +- 4 files changed, 47 insertions(+), 1 deletion(-) diff --git a/packages/webview_flutter/webview_flutter_android/CHANGELOG.md b/packages/webview_flutter/webview_flutter_android/CHANGELOG.md index 986f9882ee3..90bd5aa27e5 100644 --- a/packages/webview_flutter/webview_flutter_android/CHANGELOG.md +++ b/packages/webview_flutter/webview_flutter_android/CHANGELOG.md @@ -1,3 +1,7 @@ +## 4.10.4 + +* Adds a README section about enabling geolocation. + ## 4.10.3 * Updates Java compatibility version to 17. diff --git a/packages/webview_flutter/webview_flutter_android/README.md b/packages/webview_flutter/webview_flutter_android/README.md index a87a4ace48f..ef73e5299be 100644 --- a/packages/webview_flutter/webview_flutter_android/README.md +++ b/packages/webview_flutter/webview_flutter_android/README.md @@ -106,6 +106,29 @@ androidController.setCustomWidgetCallbacks( ); ``` +## Geolocation + +By default, WebView does not allow geolocation requests. To allow them, call +`setGeolocationPermissionsPromptCallbacks` on the `AndroidWebViewController` to +configure a prompt handler. For example, to unconditionally allow all requests: + + +```dart +await androidController.setGeolocationPermissionsPromptCallbacks( + onShowPrompt: (GeolocationPermissionsRequestParams request) async { + return const GeolocationPermissionsResponse(allow: true, retain: true); + }, +); +``` + +**Important:** Geolocation requests should only be allowed unconditionally if +the web view content is restricted to domains you control or trust. If you are +showing untrusted content, the `onShowPrompt` implementation should request +permission from the user before responding. + +Your application must have geolocation permissions granted in order for the +WebView to have access to geolocation. + ## Contributing For information on contributing to this plugin, see [`CONTRIBUTING.md`](CONTRIBUTING.md). diff --git a/packages/webview_flutter/webview_flutter_android/example/lib/readme_excerpts.dart b/packages/webview_flutter/webview_flutter_android/example/lib/readme_excerpts.dart index e925f5f22d7..4ae6269ca28 100644 --- a/packages/webview_flutter/webview_flutter_android/example/lib/readme_excerpts.dart +++ b/packages/webview_flutter/webview_flutter_android/example/lib/readme_excerpts.dart @@ -21,3 +21,22 @@ Future enablePaymentRequest() async { } // #enddocregion payment_request_example } + +/// Example function for README demonstration of geolocation permissions for +/// a use case where the content is always trusted (for example, it only shows +/// content from a domain controlled by the app developer) and geolocation +/// should always be allowed. +Future setGeolocationPermissionsPrompt() async { + final PlatformWebViewController controller = PlatformWebViewController( + AndroidWebViewControllerCreationParams(), + ); + final AndroidWebViewController androidController = + controller as AndroidWebViewController; + // #docregion geolocation_example + await androidController.setGeolocationPermissionsPromptCallbacks( + onShowPrompt: (GeolocationPermissionsRequestParams request) async { + return const GeolocationPermissionsResponse(allow: true, retain: true); + }, + ); + // #enddocregion geolocation_example +} diff --git a/packages/webview_flutter/webview_flutter_android/pubspec.yaml b/packages/webview_flutter/webview_flutter_android/pubspec.yaml index 5657dde8e98..fbfa78e3ef7 100644 --- a/packages/webview_flutter/webview_flutter_android/pubspec.yaml +++ b/packages/webview_flutter/webview_flutter_android/pubspec.yaml @@ -2,7 +2,7 @@ name: webview_flutter_android description: A Flutter plugin that provides a WebView widget on Android. repository: https://github.com/flutter/packages/tree/main/packages/webview_flutter/webview_flutter_android issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+webview%22 -version: 4.10.3 +version: 4.10.4 environment: sdk: ^3.9.0