Skip to content

Commit 16f88c3

Browse files
[webview_flutter] Add Android geolocation README (#10166)
Adds a section to `webview_flutter_android`'s README explaining how to enable geolocation. Fixes flutter/flutter#173279 ## Pre-Review Checklist [^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
1 parent 0826e11 commit 16f88c3

File tree

4 files changed

+47
-1
lines changed

4 files changed

+47
-1
lines changed

packages/webview_flutter/webview_flutter_android/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 4.10.4
2+
3+
* Adds a README section about enabling geolocation.
4+
15
## 4.10.3
26

37
* Updates Java compatibility version to 17.

packages/webview_flutter/webview_flutter_android/README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,29 @@ androidController.setCustomWidgetCallbacks(
106106
);
107107
```
108108

109+
## Geolocation
110+
111+
By default, WebView does not allow geolocation requests. To allow them, call
112+
`setGeolocationPermissionsPromptCallbacks` on the `AndroidWebViewController` to
113+
configure a prompt handler. For example, to unconditionally allow all requests:
114+
115+
<?code-excerpt "example/lib/readme_excerpts.dart (geolocation_example)"?>
116+
```dart
117+
await androidController.setGeolocationPermissionsPromptCallbacks(
118+
onShowPrompt: (GeolocationPermissionsRequestParams request) async {
119+
return const GeolocationPermissionsResponse(allow: true, retain: true);
120+
},
121+
);
122+
```
123+
124+
**Important:** Geolocation requests should only be allowed unconditionally if
125+
the web view content is restricted to domains you control or trust. If you are
126+
showing untrusted content, the `onShowPrompt` implementation should request
127+
permission from the user before responding.
128+
129+
Your application must have geolocation permissions granted in order for the
130+
WebView to have access to geolocation.
131+
109132
## Contributing
110133

111134
For information on contributing to this plugin, see [`CONTRIBUTING.md`](CONTRIBUTING.md).

packages/webview_flutter/webview_flutter_android/example/lib/readme_excerpts.dart

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,22 @@ Future<void> enablePaymentRequest() async {
2121
}
2222
// #enddocregion payment_request_example
2323
}
24+
25+
/// Example function for README demonstration of geolocation permissions for
26+
/// a use case where the content is always trusted (for example, it only shows
27+
/// content from a domain controlled by the app developer) and geolocation
28+
/// should always be allowed.
29+
Future<void> setGeolocationPermissionsPrompt() async {
30+
final PlatformWebViewController controller = PlatformWebViewController(
31+
AndroidWebViewControllerCreationParams(),
32+
);
33+
final AndroidWebViewController androidController =
34+
controller as AndroidWebViewController;
35+
// #docregion geolocation_example
36+
await androidController.setGeolocationPermissionsPromptCallbacks(
37+
onShowPrompt: (GeolocationPermissionsRequestParams request) async {
38+
return const GeolocationPermissionsResponse(allow: true, retain: true);
39+
},
40+
);
41+
// #enddocregion geolocation_example
42+
}

packages/webview_flutter/webview_flutter_android/pubspec.yaml

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

77
environment:
88
sdk: ^3.9.0

0 commit comments

Comments
 (0)