【发布时间】:2017-11-21 14:08:33
【问题描述】:
我正在使用 Xamarin WebView 控制器来显示网站(在 iOS/Android 中)。网站主页提示访问设备位置。但是,当我在 WebView(来自应用程序)上有这个网站时,没有显示该提示。当我在浏览器上打开此站点时,它会从同一移动设备显示消息框。我只是想知道是否可以在 Xamarin WebView 上有这样的提示?
这是一个示例提示。
这就是我在 Xamarin 应用程序中的全部内容。
<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>
更新 1
Android
我已经尝试了this帖子中提到的解决方案,但仍然没有显示提示。
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;
}
}
}
清单
<?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
在 plist 文件中尝试了NSLocationWhenInUseUsageDescription,但没有区别。
【问题讨论】:
标签: android ios xamarin webview xamarin.forms