Skip to content

Commit 59d155a

Browse files
authored
Merge pull request #328 from iamluciano/master
Add support for geolocation Android
2 parents 5d5316f + 8dcaf29 commit 59d155a

File tree

4 files changed

+21
-2
lines changed

4 files changed

+21
-2
lines changed

android/src/main/java/com/flutter_webview_plugin/FlutterWebviewPlugin.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ private void openUrl(MethodCall call, MethodChannel.Result result) {
9797
Map<String, String> headers = call.argument("headers");
9898
boolean scrollBar = call.argument("scrollBar");
9999
boolean allowFileURLs = call.argument("allowFileURLs");
100+
boolean geolocationEnabled = call.argument("geolocationEnabled");
100101

101102
if (webViewManager == null || webViewManager.closed == true) {
102103
webViewManager = new WebviewManager(activity);
@@ -118,7 +119,8 @@ private void openUrl(MethodCall call, MethodChannel.Result result) {
118119
scrollBar,
119120
supportMultipleWindows,
120121
appCacheEnabled,
121-
allowFileURLs
122+
allowFileURLs,
123+
geolocationEnabled
122124
);
123125
result.success(null);
124126
}

android/src/main/java/com/flutter_webview_plugin/WebviewManager.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import android.view.View;
1111
import android.view.ViewGroup;
1212
import android.webkit.CookieManager;
13+
import android.webkit.GeolocationPermissions;
1314
import android.webkit.ValueCallback;
1415
import android.webkit.WebChromeClient;
1516
import android.webkit.WebSettings;
@@ -204,7 +205,8 @@ void openUrl(
204205
boolean scrollBar,
205206
boolean supportMultipleWindows,
206207
boolean appCacheEnabled,
207-
boolean allowFileURLs
208+
boolean allowFileURLs,
209+
boolean geolocationEnabled
208210
) {
209211
webView.getSettings().setJavaScriptEnabled(withJavascript);
210212
webView.getSettings().setBuiltInZoomControls(withZoom);
@@ -219,6 +221,16 @@ void openUrl(
219221
webView.getSettings().setAllowFileAccessFromFileURLs(allowFileURLs);
220222
webView.getSettings().setAllowUniversalAccessFromFileURLs(allowFileURLs);
221223

224+
if (geolocationEnabled) {
225+
webView.getSettings().setGeolocationEnabled(true);
226+
webView.setWebChromeClient(new WebChromeClient() {
227+
@Override
228+
public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
229+
callback.invoke(origin, true, false);
230+
}
231+
});
232+
}
233+
222234
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
223235
webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE);
224236
}

lib/src/base.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ class FlutterWebviewPlugin {
110110
bool supportMultipleWindows,
111111
bool appCacheEnabled,
112112
bool allowFileURLs,
113+
bool geolocationEnabled,
113114
}) async {
114115
final args = <String, dynamic>{
115116
'url': url,
@@ -126,6 +127,7 @@ class FlutterWebviewPlugin {
126127
'supportMultipleWindows': supportMultipleWindows ?? false,
127128
'appCacheEnabled': appCacheEnabled ?? false,
128129
'allowFileURLs': allowFileURLs ?? false,
130+
'geolocationEnabled': geolocationEnabled ?? false,
129131
};
130132

131133
if (headers != null) {

lib/src/webview_scaffold.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class WebviewScaffold extends StatefulWidget {
3030
this.hidden = false,
3131
this.initialChild,
3232
this.allowFileURLs,
33+
this.geolocationEnabled
3334
}) : super(key: key);
3435

3536
final PreferredSizeWidget appBar;
@@ -52,6 +53,7 @@ class WebviewScaffold extends StatefulWidget {
5253
final bool hidden;
5354
final Widget initialChild;
5455
final bool allowFileURLs;
56+
final bool geolocationEnabled;
5557

5658
@override
5759
_WebviewScaffoldState createState() => _WebviewScaffoldState();
@@ -115,6 +117,7 @@ class _WebviewScaffoldState extends State<WebviewScaffold> {
115117
supportMultipleWindows: widget.supportMultipleWindows,
116118
appCacheEnabled: widget.appCacheEnabled,
117119
allowFileURLs: widget.allowFileURLs,
120+
geolocationEnabled: widget.geolocationEnabled
118121
);
119122
} else {
120123
if (_rect != value) {

0 commit comments

Comments
 (0)