https access implementation of xamarin WebView [Memo]

I recently made a project using xamarin, which is based on a hybrid implementation of Web + APP. HTTP access can be directly accessed by webview and WKWebview of android and ios.


By 2017, APPLE will be required to support https communication, so webview and wkwebview need to support https access, especially https with self-created certificates.


First try direct access:

1. ios: Directly use https to access. Unexpectedly, it was abnormal before. There was no prompt such as the site certificate expiration. It was blank, and there was no prompt when refreshing.

2. android: also blank.


OK, let’s search through articles on ios and android’s webview supporting https and find the answer:

Android is relatively simple:

Rewrite: Android.Webkit.WebViewClient class and let it be hosted. The code is as follows:

class CWebViewClient: Android.Webkit.WebViewClient{

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

        }

IOS, the principle is similar, the code:

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 key inheritance: IWKNavigationDelegate interface, then export [Export("webView:didReceiveAuthenticationChallenge:completionHandler:")] and override the method.


Guess you like

Origin blog.csdn.net/Jockey/article/details/53838126