【问题标题】:Xamarin forms ios webview customRenderer to basic authenticationXamarin 将 ios webview customRenderer 形成为基本身份验证
【发布时间】:2018-11-13 05:01:19
【问题描述】:

我正在尝试使用 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;
    }
}

谢谢

【问题讨论】:

    标签: xamarin xamarin.forms xamarin.ios


    【解决方案1】:

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

    首先,在表单中创建一个具有可绑定属性的自定义 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 中使用这个 webView:

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

    【讨论】:

    • 它什么也没显示,看起来还可以,但不是,如果我用常规的 webview 设置源属性而不是您的自定义 uri 属性替换,它可以工作,当然没有身份验证。任何事物? tks
    • 是的,您只需要设置此 uri 即可显示您的内容。 DidReceiveAuthenticationChallenge() 事件是否调用?
    • 更正过去的评论,设置 uri 在 webview 控件中打开页面(您的代码有效),即使在 google.com 中调用了事件 DidReceiveAuthenticationChallenge,但在我自己的 url 中未调用该事件。我的网址是http,应该是什么?
    • 请使用 https 请求进行测试,当 web 视图需要响应身份验证质询时调用此事件。所以你要加载的 url 需要是一个安全请求。
    • 实际上我的 url 指向一个网络摄像头(视频流)并且不支持 https,但在 Chrome 等常规浏览器中,网络摄像头的 url 会弹出登录表单。我被卡住了......
    【解决方案2】:

    您可以在 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;
        }
    }
    

    【讨论】:

    • 似乎您链接了这些点,但 customWebView 没有显示任何内容,甚至没有显示任何简单的网站 url,没有 autentication,还有什么其他的吗? tks
    猜你喜欢
    • 2018-08-27
    • 2020-06-26
    • 1970-01-01
    • 2019-09-27
    • 2013-03-19
    • 2011-03-22
    • 1970-01-01
    • 1970-01-01
    • 2021-06-20
    相关资源
    最近更新 更多