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:")] ,重写方法。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值