Skip to content

Instantly share code, notes, and snippets.

@Cheesebaron
Last active March 30, 2021 20:51
Show Gist options
  • Save Cheesebaron/ad84740c9bffa7e255c8 to your computer and use it in GitHub Desktop.
Save Cheesebaron/ad84740c9bffa7e255c8 to your computer and use it in GitHub Desktop.
WebView Location prompt sample
using Android;
using Android.App;
using Android.Content;
using Android.Webkit;
using Android.OS;
[assembly: UsesPermission(Name = Manifest.Permission.AccessFineLocation)]
[assembly: UsesPermission(Name = Manifest.Permission.Internet)]
namespace WebViewLocation
{
[Activity(Label = "WebView Location", MainLauncher = true, Icon = "@drawable/icon")]
public class WebViewLocationActivity : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
var webview = new WebView(this);
webview.Settings.JavaScriptEnabled = true;
webview.Settings.SetGeolocationEnabled(true);
webview.SetWebViewClient(new MyWebViewClient());
webview.SetWebChromeClient(new MyWebChromeClient(this));
webview.LoadUrl("https://maps.google.com");
SetContentView(webview);
}
}
public class MyWebViewClient : WebViewClient
{
public override bool ShouldOverrideUrlLoading(WebView view, string url)
{
view.LoadUrl(url);
return true;
}
}
public class MyWebChromeClient : WebChromeClient
{
private readonly Context _context;
public MyWebChromeClient(Context context)
{
_context = context;
}
public override void OnGeolocationPermissionsShowPrompt(string origin, GeolocationPermissions.ICallback callback)
{
const bool remember = false;
var builder = new AlertDialog.Builder(_context);
builder.SetTitle("Location")
.SetMessage(string.Format("{0} would like to use your current location", origin))
.SetPositiveButton("Allow", (sender, args) => callback.Invoke(origin, true, remember))
.SetNegativeButton("Disallow", (sender, args) => callback.Invoke(origin, false, remember));
var alert = builder.Create();
alert.Show();
}
}
}
@JohnDoeMan
Copy link

Hi,

First off all, thanks for the code.

I've been trying to put htis to work with no luck so far.
When I publish the app into my Android 6.0 phone everything goes well with the webview but the geolocation simply does not work.

Testing URL: https://www.google.pt/maps/
Error: E/cr_LocationProvider(28095): Caught security exception while registering for location updates from the system. The application does not have sufficient geolocation permissions.
Phones:
Sony Xperia E5
Sony Xperia E3
AndroidManifest:
uses-permission android:name="android.permission.INTERNET"
uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"
uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"
uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"
I can't seem to find any more info on this.

Could you help figure out what's wrong with it.

Thank you

@bobbymale
Copy link

Code works perfectly but when webview is refreshed, it prompt the user again. Can the permission allowed to be kept?

@Logicbloke
Copy link

Make the remember constant true and it should stick.

Copy link

ghost commented Mar 30, 2021

It doesn't work for me [u_u]

@Cheesebaron
Copy link
Author

@josedavidpc310 too bad. This is a 4 year old sample. Things have changed since then.

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