xamarin MAUI Webview target ="_blank" href重定向在android上不工作

qvtsj1bj  于 2022-12-07  发布在  Android
关注(0)|答案(2)|浏览(261)

我有一个MAUI应用程序,本质上是一个托管WordPress网站的WebView。我的问题是,每当我试图加载外部链接时,它都不会加载“href”重定向...它在Windows和iOS设备上运行良好,但在Android中不是。
EDIT:href是一个“_blank”目标。
清单中是否有我需要的特定权限?
下面是该页面的代码:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MYAPPNAME.EnglishPage" 
             NavigationPage.HasNavigationBar="False" NavigationPage.HasBackButton="False"
             Shell.NavBarIsVisible="False">
    <Grid BackgroundColor="DarkSlateGrey">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <StackLayout Grid.Row="0" Grid.Column="0" 
                         Spacing="5" Orientation="Horizontal" Padding="8">
            <Button Text="Go Back" TextColor="DarkSlateGray" BackgroundColor="White"/>
            <Button Text="Change Language" x:Name="ChangeLangBtn" TextColor="DarkSlateGray" BackgroundColor="White"/>
        </StackLayout>
        <WebView Grid.Row="1" Grid.Column="0" 
                     Source="https://example.com"
                     x:Name="WebView"/>
    </Grid>
</ContentPage>

这是清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="2" android:versionName="2">
    <application android:allowBackup="true" android:icon="@mipmap/appicon" android:roundIcon="@mipmap/appicon_round" android:supportsRtl="true" android:label="MYAPPNAME"></application>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-sdk android:minSdkVersion="21" android:targetSdkVersion="31" />
</manifest>
gudnpqoy

gudnpqoy1#

我已经做了一个例子来测试在android的webview,它可以成功地打开在网站中的href。
所以你可以检查你要打开的href是否是http,对于27以上的android api,你需要在AndroidManifest.xml中声明android:usesCleartextTraffic为true,比如:

<manifest ...>
  <application
    ...
    android:usesCleartextTraffic="true"
    ...>
    ...
  </application>
  <uses-permission android:name="android.permission.INTERNET" />
</manifest>

您也可以尝试在您的webview中加载另一个网站进行测试。

编辑

引用这个native android case和这个custom webview handler,你就可以试着用这个句柄重写ShouldOverrideUrlLoading方法来做到这一点。

5rgfhyps

5rgfhyps2#

So to handle redirects to _blank targets in a WebView by keeping them in the same window/view, I had to create a custom renderer, but don't worry, you can just copy-paste the code and it will work perfectly :) .

  1. Create a folder called Controls inside Platforms/Android , then create a class file, I called it MltWebViewClientRenderer.cs . Paste this inside the file:
public class MltWebViewRenderer : WebViewRenderer
    {
        public MltWebViewRenderer(Context context) : base(context)
        {
        }
        protected override void OnElementChanged(ElementChangedEventArgs<Microsoft.Maui.Controls.WebView> e)
        {
            base.OnElementChanged(e);
            Control.SetWebViewClient(new MltWebViewClient());
            global::Android.Webkit.WebView.SetWebContentsDebuggingEnabled(true);
        }
    }

    public class MltWebViewClient : global::Android.Webkit.WebViewClient
    {
        public MltWebViewClient()
        {
        }

        public override bool ShouldOverrideUrlLoading(global::Android.Webkit.WebView view, IWebResourceRequest request)
        {
            view.Settings.SetSupportMultipleWindows(false);
            view.Settings.JavaScriptCanOpenWindowsAutomatically = true;
            
            view.Settings.JavaScriptEnabled = true;

            view.SetWebChromeClient(new WebChromeClient());

            return base.ShouldOverrideUrlLoading(view, request);
        }
    }

The above code basically allows access to the Settings of the WebView .

  1. In the MauiProgram.cs , you need to add some services to the builder, as such:
var builder = MauiApp.CreateBuilder();
            builder
                .UseMauiApp<App>()
                .ConfigureFonts(fonts =>
                {
                    fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
                    fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
                })
                .UseMauiCompatibility()
                .ConfigureMauiHandlers(x =>
                {
#if ANDROID
                    x.AddCompatibilityRenderer(typeof(WebView), typeof(Platforms.Android.Controls.MltWebViewRenderer));
#endif
                });
            return builder.Build();

The above only invokes our custom renderer if we are on Android.
Note: This solution only works for Android API versions 24 and above.

相关问题