Xamarin формирует ios webview customRenderer для базовой аутентификации - PullRequest
0 голосов
/ 03 июня 2018

Я пытаюсь использовать базовую аутентификацию с Xamarin Forms, особенно для iOS.

У меня есть собственный рендерер WebView для достижения этой цели.

Я видел этот пример в Xamarin.ios;как я могу заставить его работать в моем пользовательском рендерере?

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;
    }
}

Спасибо

Ответы [ 2 ]

0 голосов
/ 05 июня 2018

Кажется, вы хотите использовать WKWebView и WKNavigationDelegate в Xamarin.Forms.

Во-первых, создайте пользовательский WebView с привязываемым свойством в формах:

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, чтобы показатьвеб-страница:

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:

<local:CustomWebView Uri="https://www.microsoft.com" HeightRequest="300"/>
0 голосов
/ 04 июня 2018

вы можете достичь с помощью пользовательского рендерера в Xamarin.Forms

Для iOS

    public class CustomWebViewRenderer : ViewRenderer<CustomWebView,WKWebView>
{
    WKWebView webView;
    public CustomWebViewRenderer()
    {
    }

    protected override void OnElementChanged(ElementChangedEventArgs<CustomWebView> e)
    {
        base.OnElementChanged(e);
        webView = new WKWebView(Frame, new WKWebViewConfiguration());
        webView.NavigationDelegate = new MyWeakNavigationDelegate(this);
        SetNativeControl(webView);
    }

}

internal class MyWeakNavigationDelegate : WKNavigationDelegate, INSUrlConnectionDataDelegate
{
    private CustomWebViewRenderer customWebViewRenderer;

    public MyWeakNavigationDelegate(CustomWebViewRenderer customWebViewRenderer)
    {
        this.customWebViewRenderer = customWebViewRenderer;
    }

    public override void DidReceiveAuthenticationChallenge(WKWebView webView, NSUrlAuthenticationChallenge challenge, Action<NSUrlSessionAuthChallengeDisposition, NSUrlCredential> completionHandler)
    {
        completionHandler(NSUrlSessionAuthChallengeDisposition.UseCredential, new NSUrlCredential("username", "password", NSUrlCredentialPersistence.ForSession));
        Console.WriteLine("We are authenticated");
        return;
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...