1

I have created a WKWebView. It loads the url of MSB ie. My school bucks. It loads almost all url for payment. But at one url didFinishLoad is not getting called. I have added all the delegates for the web view.but certainly it is not working. The whole process works fine in the web browser of iPhone.

Below is the code I have added below code for that

let preferences = WKPreferences()
        preferences.javaScriptEnabled = true
        let configuration = WKWebViewConfiguration()
        configuration.preferences = preferences
        self.webView = WKWebView(frame: frame, configuration: configuration)
        self.view.addSubview(webView)
        self.webView.navigationDelegate = self
        self.webView.uiDelegate = self

Delegates Methods for the navigation

// MARK: - WKWebView Delegation
extension PaymentWebViewController:WKNavigationDelegate {

    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        DILog.print(items: "didFinish")
        self.hideLoader()
        if let strUrl = webView.url?.absoluteString {
            self.readRedirectUrl(stringUrl: strUrl)
        }
    }

    func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
        DILog.print(items: "faluere")
        self.hideLoader()
    }

    func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
        DILog.print(items: "didStatrt")
        self.showLoader()
        if let strUrl = webView.url?.absoluteString {
            DILog.print(items: "URL IS \(strUrl)")
        }
    }

    func webView(_ webView: WKWebView,
                 didReceive challenge: URLAuthenticationChallenge,
                 completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void)
    {
        if(challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust)
        {        DILog.print(items: "Auth Channlenge1")
            let cred = URLCredential(trust: challenge.protectionSpace.serverTrust!)
            completionHandler(.useCredential, cred)
        }
        else
        {        DILog.print(items: "Auth Channlenge2")
            completionHandler(.performDefaultHandling, nil)
        }
    }

    func webViewWebContentProcessDidTerminate(_ webView: WKWebView) {
        DILog.print(items: "webViewWebContentProcessDidTerminate")

    }

    func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) {
        DILog.print(items: "didCommit")

    }

    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
        DILog.print(items: "decidePolicyFor Action")
        decisionHandler(WKNavigationActionPolicy.allow)
    }

    func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void) {
        DILog.print(items: "decidePolicyFor response ")

        decisionHandler(.allow)
    }
}
TechChain
  • 8,404
  • 29
  • 103
  • 228

1 Answers1

0

You need to add exception (SecTrustSetExceptions) for that particular url. As there is some conflict with its signing identity and your WKWebView.

I was also facing same issue in my app and successfully solved by using below snippet.

try this:

    extension PaymentWebViewController: WKNavigationDelegate {
    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
        decisionHandler(.allow)
    }

    func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void) {
        decisionHandler(.allow)
    }

    func webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
        let serverTrust = challenge.protectionSpace.serverTrust
        let exceptions = SecTrustCopyExceptions(serverTrust!)
        SecTrustSetExceptions(serverTrust!, exceptions)
        completionHandler(.useCredential, URLCredential(trust: serverTrust!))
    }
}

Hope you've already setup this in your info.plist:

    <key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoadsInWebContent</key>
    <true/>
</dict>
Milan Manwar
  • 374
  • 3
  • 10
  • Thanks. but I get below error provison An SSL error has occurred and a secure connection to the server cannot be made. How can Ignore ssl ? – TechChain Aug 21 '18 at 12:06
  • This solution is designed only to add an exception to ssl failure. Your SSL error should be fixed with this, if you've tried implementing this code snippet. – Milan Manwar Aug 21 '18 at 12:26
  • then check your info.plist for ATS configuration made by you. Also if possible you can share url here. – Milan Manwar Aug 21 '18 at 12:30
  • NSAllowsArbitraryLoadsInWebContent by doing this does not allow me to make a http request over non ssl server. Do I need to add domain exceptions? – TechChain Aug 21 '18 at 12:33
  • @Techiee It'll allow you to load http resources after adding this NSAllowsArbitraryLoadsInWebContent and set it to true. I think you misunderstood this. – Milan Manwar Aug 21 '18 at 12:35
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/178446/discussion-between-milan-manwar-and-techiee). – Milan Manwar Aug 21 '18 at 12:35
  • can you help me with something ? – TechChain Aug 22 '18 at 04:27