xamarin WebView的https 访问实现[备忘]

最近用xamarin 做了个项目,基于Web +APP的混合实现,对于http 访问是可以让android 和 ios 的webview 和WKWebview 直接访问。


到2017年APPLE就强制要求支持https的通讯,因此需要让webview 和 wkwebview 支持https的访问,尤其是自建证书的https。


先试一下直接访问:

1、ios: 直接使用https访问,意料之前的不正常,连站点证书过期之类的提示也没有,直接空白,刷新也一样不会有任何提示。

2、android :也是空白。


OK,百度一下ios 和android 的 webview 支持https 的文章,找到答案:

android的相对简单很多:

重写:Android.Webkit.WebViewClient 类,让它托管,代码如下:

class CWebViewClient: Android.Webkit.WebViewClient{

        public override  void OnReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
            handler.Proceed();//直接通过。        

        }

IOS,原理也差不多,代码:

public  class CWKNavigationDelegate : WKUserContentController, IWKNavigationDelegate , INSUrlConnectionDataDelegate

    {



        [Export("webView:didReceiveAuthenticationChallenge:completionHandler:")]
        public virtual void DidReceiveAuthenticationChallenge(WKWebView webView , NSUrlAuthenticationChallenge nac, Action<NSUrlSessionAuthChallengeDisposition, NSUrlCredential>  NC)
        {


            if (nac.ProtectionSpace.AuthenticationMethod.Equals("NSURLAuthenticationMethodServerTrust"))
            {
                nac.Sender.UseCredential(new NSUrlCredential (  nac.ProtectionSpace.ServerSecTrust), nac);
                nac.Sender.ContinueWithoutCredential(nac);
            }

}

 }

IOS关鍵继承:IWKNavigationDelegate 接口,然后导出[Export("webView:didReceiveAuthenticationChallenge:completionHandler:")] ,重写方法。


猜你喜欢

转载自blog.csdn.net/Jockey/article/details/53838126
今日推荐