WkWebView Check Url Swift 4


You might required to implement the following in your code, which means instead of using UIWebViewDelegate protocol try to use WKNavigationDelegate protocol. I guess you are missing one of the most important function when you are handling with sessions.



    import UIKit
import WebKit

class PusatBantuanViewController: UIViewController, WKNavigationDelegate {
    
    lazy var webView: WKWebView = {
        let web = WKWebView()
        web.backgroundColor = UIColor.white
        web.isUserInteractionEnabled = true
        web.scrollView.showsHorizontalScrollIndicator = false
        web.scrollView.showsVerticalScrollIndicator = false
        web.translatesAutoresizingMaskIntoConstraints = false
        return web
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.navigationController?.isNavigationBarHidden = true
        webView.navigationDelegate = self
        
        let url = URL(string: "https://yourUrl")
        let request = URLRequest(url: url!)
        self.webView.load(request)
        self.view.addSubview(self.webView)
        self.webView.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 8).isActive = true
        self.webView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: 0).isActive = true
        self.webView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor, constant: 0).isActive = true
        self.webView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor, constant: 0).isActive = true
        
    }

- And this how i check url in WkWebview


func webViewWebContentProcessDidTerminate(_ webView: WKWebView) {
        print("1")
        print("test: \(webView.url)")
    }
    
    func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) {
        print("2")
        print("test: \(webView.url)")
    }
    
    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
           print("3")
        print("test: \(webView.url)")
    }
    
    func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
           print("4")
        print("test: \(webView.url)")
    }
    
    func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
           print("5")
        print("test: \(webView.url)")
    }
    
    func webView(_ webView: WKWebView, didReceiveServerRedirectForProvisionalNavigation navigation: WKNavigation!) {
           print("6")
        print("test: \(webView.url)")
    }
    
    func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
           print("7")
        print("test: \(webView.url)")
    }
    
    func webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
           print("8")
        print("test: \(webView.url)")
        completionHandler(.performDefaultHandling,nil)
    }

    func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void) {
        print("9")
        print("test: \(webView.url)")
        decisionHandler(.allow)
    }

Update For Check Loading is Finish:

No comments: