【问题标题】:Enabling basic authentication in webview without custom WebViewClient in Xamarin Forms在没有 Xamarin Forms 中的自定义 WebViewClient 的情况下在 webview 中启用基本身份验证
【发布时间】:2022-12-17 10:05:04
【问题描述】:

我在我的 Xamarin Forms 项目中使用 Hybrid Rendererwebview 的 webview,因为我必须在页面内注入 javascript 代码。

在我的主要项目中,我有一个CustomWebview.cs

namespace ClotureSiadForms.Renderer
{
    public class CustomWebView : WebView
    {
        public string js = "";

        public CustomWebView()
        {
            Navigating+= WebViewNavigating;
            Navigated+=WebViewNavigated;
        }

        private void WebViewNavigated(object sender, WebNavigatedEventArgs args)
        {
            EvaluateJavaScriptAsync(js);
        }

        public void WebViewNavigating(object sender, WebNavigatingEventArgs args)
        {
            if (args.Url.StartsWith("tel:"))
            {
                var tel = args.Url.Split(':')[1];
                args.Cancel = true;

                Xamarin.Essentials.PhoneDialer.Open(tel);
            }
            else if (!args.Url.StartsWith("http") || args.Url.EndsWith(".apk") || args.Url.EndsWith(".pdf") || args.Url.EndsWith(".zip"))
            {
                args.Cancel = true;
                Xamarin.Essentials.Launcher.OpenAsync(args.Url);
            }
        }
    }
}

在我的 Android 项目中,我有一个 HybridWebViewRenderer.cs

[assembly: ExportRenderer(typeof(CustomWebView), typeof(HybridWebViewRenderer))]
namespace ClotureSiadForms.Droid.Renderer
{
    internal class HybridWebViewRenderer : WebViewRenderer
    {
        public HybridWebViewRenderer(Context context) : base(context)
        {
        }
        protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.WebView> e)
        {
            base.OnElementChanged(e);

            if (Control != null)
            {
                CustomWebView webview = e.NewElement as CustomWebView;

                Control.Settings.JavaScriptEnabled = true;
                Control.Settings.DomStorageEnabled = true;
                Control.Settings.SavePassword = true;
            }
        }
    }
}

照原样,它可以工作并且能够下载文件 但是因为我需要基本身份验证,所以我在HybridWebViewRenderer.cs 中添加了一个自定义的 webviewclient:

    protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.WebView> e)
    {
        base.OnElementChanged(e);

        if (Control != null)
        {
            CustomWebView webview = e.NewElement as CustomWebView;

            Control.Settings.JavaScriptEnabled = true;
            Control.Settings.DomStorageEnabled = true;
            Control.Settings.SavePassword = true;

            var login = Preferences.Get("login", "");
            var password = Preferences.Get("password", "");
            Control.SetWebViewClient(new AuthWebViewClient(login, password));
        }
    }
    public class AuthWebViewClient : WebViewClient
    {
        private string Username;
        private string Password;
        public AuthWebViewClient(string username, string password)
        {
            Username = username;
            Password = password;
        }
        public override void OnReceivedHttpAuthRequest(Android.Webkit.WebView view, HttpAuthHandler handler, string host, string realm)
        {
            handler.Proceed( Username,Password);
        }
    }

身份验证有效,但现在调用一次WebViewNavigating,然后设置自定义客户端,然后再也不会调用WebViewNavigating

那么我的问题是,我不能在没有自定义客户端的情况下使用基本身份验证,还是有办法继续在客户端使用我的customwebview

【问题讨论】:

    标签: xamarin.forms


    【解决方案1】:

    并且身份验证有效,但是现在调用一次WebViewNavigating,然后设置自定义客户端,然后再也不会调用WebViewNavigating

    我测试了您提供的代码并将断点添加到 WebViewNavigating 方法。即使你不加webviewclient,它也只会调用一次WebViewNavigating

    你可以把代码放在WebViewNavigatingShouldInterceptRequest

     public class AuthWebViewClient : WebViewClient
        {
            ...
            public override WebResourceResponse ShouldInterceptRequest(Android.Webkit.WebView view, IWebResourceRequest request)
            {
                var url = request.Url;
                ...
            }
        }
    

    每当 WebView 开始加载新页面时,它都会调用 ShouldInterceptRequest

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-06
      • 2022-01-09
      • 2020-06-24
      • 1970-01-01
      • 1970-01-01
      • 2022-01-03
      • 2013-11-10
      • 2019-09-21
      相关资源
      最近更新 更多