gpt4 book ai didi

android - 显示提示以了解位置

转载 作者:技术小花猫 更新时间:2023-10-29 11:05:41 26 4
gpt4 key购买 nike

我正在使用 Xamarin WebView Controller 来显示网站(在 iOS/Android 中)。该网站的主页提示访问设备位置。但是当我在 WebView 上(从应用程序)上有这个网站时,这个提示没有显示。当我在浏览器上打开此站点时,它会从同一台移动设备显示消息框。我只是想知道是否可以在 Xamarin WebView 上有这样的提示?

这是一个示例提示。

enter image description here

这就是我在 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;
}
}
}

list

<?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 - 您需要为 WebView 控件自定义渲染器。 (引用:gist)

public class ExtendedWebViewRenderer : WebViewRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.WebView> e)
{
base.OnElementChanged(e);

if (e.OldElement != null || Element == null)
{
return;
}

Control.Settings.JavaScriptEnabled = true;
Control.Settings.SetGeolocationEnabled(true);
Control.Settings.SetGeolocationDatabasePath(Control.Context.FilesDir.Path);

Control.SetWebViewClient(new CustomWebViewClient());
Control.SetWebChromeClient(new CustomChromeClient(Control.Context));
}
}

public class CustomWebViewClient : WebViewClient
{
public override bool ShouldOverrideUrlLoading(Android.Webkit.WebView view, string url)
{
view.LoadUrl(url);
return true;
}
}

public class CustomChromeClient : WebChromeClient
{
private readonly Context _context;

public CustomChromeClient(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();
}
}

list 权限

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />

使用示例

<StackLayout Margin="30" BackgroundColor="#ededed">
<Label Text="WebView location prompt sample" Margin="15" HorizontalOptions="Center" />
<local:ExtendedWebView x:Name="webView" Source="https://www.yellowpages.com/" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" />
</StackLayout>

android output

而且,如果是 iOS,您需要在 info.plist 文件中设置 NSLocationWhenInUseUsageDescription 描述。

info.plist

并显示提示。

ios output

Complete code sample can be found at this link


原始答案

对于 Android - 您应该能够通过为 WebView 添加特定于平台的渲染器并实现权限处理来解决此问题 by adding a custom WebChromeClient .

如果是 iOS - 你需要添加 NSLocationWhenInUseUsageDescription description在你的信息列表中

关于android - 显示提示以了解位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44629197/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com