Android WebView: handling Geolocation permission request
Every now and then I come across the request to create an Android “container“ app from an existing responsive website.
Though I don’t recommend to do such a thing and I always suggest to make a native mobile application for the performance and UX sake, often that’s what clients want.
That being said, more and more websites require the user’s location to provide services that best meet their needs.
Let’s see what does this mean when you load the website in a WebView.
Let us suppose we want to load the geolocation sample from Google Maps API docs into your WebView.
What will happen in a regular web browser is something like this:
the web browser will ask the user for permission to access his/her location.
This don’t automatically happen with a WebView since you have to handle this permission request the Android way in your code.
If your WebView has an associated WebChromeClient when a website attempts to use Geolocation API this will receive a notification via the method
onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback)