Xamarin 将 ios webview customRenderer 表单为基本身份验证 [英] Xamarin forms ios webview customRenderer to basic authentication

查看:19
本文介绍了Xamarin 将 ios webview customRenderer 表单为基本身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将基本身份验证与 Xamarin Forms 结合使用,特别适用于 iOS.

I am trying to use basic authentication with Xamarin Forms, for iOS specifically.

我有一个自定义的 WebView 渲染器来实现这一点.

I have a custom WebView renderer to achieve this.

我在 Xamarin.ios 中看到了这个示例;我怎样才能让它在我的自定义渲染器中工作?

I saw this sample in Xamarin.ios; how can i get it working in my custom renderer?

public class WebViewDelegate : WKNavigationDelegate, INSUrlConnectionDataDelegate
{

    public override void DidReceiveAuthenticationChallenge(WKWebView webView, NSUrlAuthenticationChallenge challenge, Action<NSUrlSessionAuthChallengeDisposition, NSUrlCredential> completionHandler)
    {
        //base.DidReceiveAuthenticationChallenge(webView, challenge, completionHandler);
        completionHandler(NSUrlSessionAuthChallengeDisposition.UseCredential, new NSUrlCredential("username", "password", NSUrlCredentialPersistence.ForSession));
        Console.WriteLine("We are authenticated");
        return;
    }
}

谢谢

推荐答案

您似乎想在 Xamarin.Forms 中使用 WKWebViewWKNavigationDelegate.

It seems you want to use WKWebView and WKNavigationDelegate in the Xamarin.Forms.

首先,在表单中创建一个具有可绑定属性的自定义 WebView:

Firstly, create a custom WebView with a bindable property in the forms:

public class CustomWebView : WebView
{
    public static readonly BindableProperty UriProperty = BindableProperty.Create(
        propertyName: "Uri",
        returnType: typeof(string),
        declaringType: typeof(CustomWebView),
        defaultValue: default(string));

    public string Uri
    {
        get { return (string)GetValue(UriProperty); }
        set { SetValue(UriProperty, value); }
    }
}

然后在自定义渲染器中,我们可以得到这个uri并将其设置为新的WKWebView来显示网页:

Then in the custom renderer, we can get this uri and set it to the new WKWebView to show the web page:

public class MyCustomWebViewRenderer : ViewRenderer<CustomWebView, WKWebView>
{
    protected override void OnElementChanged(ElementChangedEventArgs<CustomWebView> e)
    {
        base.OnElementChanged(e);

        if (Control == null)
        {
            var webView = new WKWebView(Frame, new WKWebViewConfiguration());
            webView.NavigationDelegate = new WebViewDelegate();
            SetNativeControl(webView);
        }
        if (e.NewElement != null)
        {
            Control.LoadRequest(new NSUrlRequest(new NSUrl(Element.Uri)));
        }
    }
}
public class WebViewDelegate : WKNavigationDelegate, INSUrlConnectionDataDelegate
{
    public override void DidReceiveAuthenticationChallenge(WKWebView webView, NSUrlAuthenticationChallenge challenge, Action<NSUrlSessionAuthChallengeDisposition, NSUrlCredential> completionHandler)
    {
        //base.DidReceiveAuthenticationChallenge(webView, challenge, completionHandler);
        completionHandler(NSUrlSessionAuthChallengeDisposition.UseCredential, new NSUrlCredential("username", "password", NSUrlCredentialPersistence.ForSession));
        Console.WriteLine("We are authenticated");
        return;
    }
}

最后你可以在 XAML 中使用这个 webView:

At last you can use this webView in the XAML:

<local:CustomWebView Uri="https://www.microsoft.com" HeightRequest="300"/>

这篇关于Xamarin 将 ios webview customRenderer 表单为基本身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆