Tuesday, August 1, 2017

Show Prompt to know the location

Leave a Comment

I am using Xamarin WebView controller to display a web site (in both iOS/Android). The web site's home page prompts to access the device location. But that prompt is not showing when I have this website on a WebView (from the App). When I open this site on the browser, from the same mobile device it shows the message box. I am just wondering if it is possible to have such prompts on Xamarin WebView?

Here is an example prompt.

enter image description here

This is all I have in the Xamarin app.

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"              xmlns:local="clr-namespace:nCollar"              x:Class="nCollar.MainPage">     <ContentPage.Padding>         <OnPlatform x:TypeArguments="Thickness">             <OnPlatform.iOS>0,20,0,0</OnPlatform.iOS>             <OnPlatform.Android>0,0,0,0</OnPlatform.Android>             <OnPlatform.WinPhone>0,0,0,0</OnPlatform.WinPhone>         </OnPlatform>     </ContentPage.Padding>      <WebView x:Name="webView"                  Grid.Row="0"                  VerticalOptions="FillAndExpand"                   Source="https://www.ncollar.com.au/"/>  </ContentPage> 

UPDATE 1

Android

I've tried the solution mentioned in this post, but still it doesn't show the prompt.

WebViewRenderer.cs

using Android.Webkit; using Xamarin.Forms.Platform.Android;  [assembly: Xamarin.Forms.ExportRenderer(typeof(TestApp.Controls.WebView), typeof(TestApp.Droid.Renderers.WebViewRenderer))] namespace TestApp.Droid.Renderers {     public class WebViewRenderer : Xamarin.Forms.Platform.Android.WebViewRenderer     {         protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.WebView> e)         {             base.OnElementChanged(e);              if (this.Control == null)             {                 return;             }              var webView = this.Element as TestApp.Controls.WebView;             if (webView == null)             {                 return;             }              // webView. ings.JavaScriptEnabled = true;             this.Control.Settings.DomStorageEnabled = true;             this.Control.Settings.DisplayZoomControls = true;             this.Control.Settings.BuiltInZoomControls = true;             this.Control.Settings.SetGeolocationEnabled(true);             this.Control.Settings.JavaScriptCanOpenWindowsAutomatically = true;              this.Control.SetWebViewClient(new GeoWebViewClient());             this.Control.SetWebChromeClient(new GeoWebChromeClient());         }     }      public class GeoWebChromeClient : WebChromeClient     {         public override void OnGeolocationPermissionsShowPrompt(string origin, GeolocationPermissions.ICallback callback)         {             callback.Invoke(origin, true, false);         }     }      public class GeoWebViewClient : WebViewClient     {         public override bool ShouldOverrideUrlLoading(WebView view, string url)         {             view.LoadUrl(url);             return true;         }     } } 

Manifest

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android">     <uses-sdk android:minSdkVersion="15" />   <uses-permission android:name="android.permission.INTERNET" />   <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />     <application android:label="TestApp.Android"></application> </manifest> 

iOS

Tried NSLocationWhenInUseUsageDescription in the plist file, but no difference.

Example App that does a similar thing

enter image description here

3 Answers

Answers 1

In case of Android - you should be able to resolve this by adding a platform-specific renderer for WebView and implement the permission handling by adding a custom WebChromeClient.

In case of iOS - you need to add NSLocationWhenInUseUsageDescription description in your info plist

Answers 2

On Android:

According to Xamarin docs OnGeolocationPermissionsShowPrompt is used to "Notify the host application that web content from the specified origin is attempting to use the Geolocation API, but no permission state is currently set for that origin. The host application should invoke the specified callback with the desired permission state." As you can see there is nothing said about confirmation dialog. If you call "callback" without any prompt you are setting the result (I tested, permission is granted after callback is called). You should implement confirmation dialog yourself. Note: It will set permission only if you have LOCATION_ACCESS in your manifest, otherwise it will not.

On iOS:

I don't know. :-) but probably the similar thing happens there.

I wouldn't even bother to capture those requests from browser. There are "normal" methods to ask user for location permissions if your app needs them.

Answers 3

But that prompt is not showing when I have this website on a WebView (from the App).

If I got it right, you're wrapping this in an app. That's not the expected user experience from a mobile app. your app should use the platform(e.g iOS,Android) location permissions in order to get the user approval.

Xamarin will let you access Location on each platform quite differently. If the Xamarin app has access to location than the webview should inherit these permissions. So it might worth trying this recipe and try accsing location before opening the webview itself

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment