Victor Natius
Victor Natius

Reputation: 31

Some WKWebView links not clickable

I am having a challenge with a WKWebView-based iOS application: some of the links on the page are not clickable. When I try them with a Safari view controller, they work. Please can anyone help me?

This is my source code:

import UIKit
import WebKit

class ViewController: UIViewController, WKUIDelegate, WKNavigationDelegate {
    var webView: WKWebView! //declare the webview has to be optional
    var activityIndicator: UIActivityIndicatorView!


    override func loadView() {
        super.loadView()
        let webConfiguration = WKWebViewConfiguration()
        webView = WKWebView(frame: .zero, configuration: webConfiguration)
        webView.uiDelegate = self
        webView.navigationDelegate = self
        view = webView
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        let myURL = URL(string: "https://stolenandfound.com/")
        let myRequest = URLRequest(url: myURL!)
        webView.load(myRequest)


        activityIndicator = UIActivityIndicatorView() //declare the progress indicator
        activityIndicator.frame = CGRect(x: 0, y: 0, width: 50, height: 50)

        self.activityIndicator.center = CGPoint(x:self.view.bounds.size.width/2.0,y: self.view.bounds.size.height/2.0);

        activityIndicator.autoresizingMask = (UIViewAutoresizing(rawValue: UIViewAutoresizing.RawValue(UInt8(UIViewAutoresizing.flexibleRightMargin.rawValue) | UInt8(UIViewAutoresizing.flexibleLeftMargin.rawValue) | UInt8(UIViewAutoresizing.flexibleBottomMargin.rawValue) | UInt8(UIViewAutoresizing.flexibleTopMargin.rawValue))))

        //activityIndicator.center = CGPoint(x: UIScreen.main.bounds.size.width/2, y: UIScreen.main.bounds.size.height/2)
        activityIndicator.hidesWhenStopped = true

        activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.whiteLarge
        activityIndicator.color = UIColor.darkGray
        self.view.addSubview(activityIndicator)
    }



    func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
        activityIndicator.startAnimating()
    }

    func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
        print("it is an error")
        activityIndicator.stopAnimating()
        let alert = UIAlertController(title: "Network Error", message: "You have no internet coonection", preferredStyle: .alert)

        let restartAction = UIAlertAction(title: "Reload page", style: .default, handler: { (UIAlertAction) in
            self.viewDidLoad()
        })

        alert.addAction(restartAction)
        present(alert, animated: true, completion: nil)
    }




    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        activityIndicator.stopAnimating()
    }

    @IBAction func backButton(_ sender: UIBarButtonItem) {
        webView.goBack()
    }


    @IBAction func refreshButton(_ sender: UIBarButtonItem) {
        webView.reload()
    }

}

Upvotes: 2

Views: 3210

Answers (1)

Santhosh R
Santhosh R

Reputation: 1568

If the links open in a new tab in Safari, you will have to implement the WKUIDelegate function func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? and handle the request there. At that point you can load the request in the existing webView or create a new webView or open it in Safari.

func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
        guard let theURL = navigationAction.request.url else {
           return nil
        }
        if loadInCurrentWebview{
            _ = webView.load(navigationAction.request) //loads the link in the current webView
        } else if loadInNewWebview{
           return WKWebView()
        }else { 
            //loads in safari
            if #available(iOS 10.0, *) {
                UIApplication.shared.open(theURL, options: [:], completionHandler: nil)
            } else {
                UIApplication.shared.openURL(theURL)
            }
        }
    return nil
}

Upvotes: 7

Related Questions