3
votes

Is it possible to detect a back swipe in a web view when there has not been any forward navigation? The following code works fine if the user has moved off the first page but I want to know if they back swiped while on that page.

Update - This is the code I'm using after modifying it per comments. I added the navigation back function and the swipe gesture.

    class WebViewClient: UIViewController, WKUIDelegate, WKNavigationDelegate, UIGestureRecognizerDelegate
{
    var webView: WKWebView!


    public func load(pageLink page: String)
    {
        print("Opening: " + page)

        if webView != nil
        {
            if let myURL = URL(string:page)
            {
                webUrlRequest = page

                let myRequest = URLRequest(url: myURL)

                webView.load(myRequest)
             }
      }

   func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!)
    {
        print("Opened: " + webUrlRequest)
    }

    override func loadView()
    {
        let pref = WKPreferences()
        pref.javaScriptEnabled = true

        let config = WKWebViewConfiguration()
        config.preferences = pref


        webView = WKWebView(frame: .zero, configuration: config)

        webView.uiDelegate = self
        webView.navigationDelegate = self

        webView.allowsBackForwardNavigationGestures = true


        view = webView
    }

    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void)
    {
        if navigationAction.navigationType == .backForward && webView.backForwardList.backList.count == 0
        {
           print("Can't go back any more")
        }

        decisionHandler(.allow)
    }

    @objc func backNavigationFunction(_ sender: UIScreenEdgePanGestureRecognizer)
    {
        let dX = sender.translation(in: view).x
        if sender.state == .ended
        {
            let fraction = abs(dX / view.bounds.width)
            if fraction >= 0.35
            {
                print("Back swipe")
                //back navigation code here
            }
        }
    }

    override func viewDidLoad()
    {
        super.viewDidLoad()


        let swipeGesture = UIScreenEdgePanGestureRecognizer(target: self, action: #selector(backNavigationFunction(_:)))
        swipeGesture.edges = .left
        swipeGesture.delegate = self

        webView.addGestureRecognizer(swipeGesture)
    }
}
1
Have you implemented any type of GestureRecognizer? - binaryPilot84
No. Just the navigation function above. - glez

1 Answers

5
votes

I would implement a UIScreenEdgePanGestureRecognizer to handle this. Add the following to your viewDidLoad

let swipeGesture = UIScreenEdgePanGestureRecognizer(target: self, action: #selector(backNavigationFunction(_:)))
    swipeGesture.edges = .left
    swipeGesture.delegate = self
    view.addGestureRecognizer(swipeGesture)

Then, implement a function to handle the backNavigationFunction like below:

@objc func backNavigationFunction(_ sender: UIScreenEdgePanGestureRecognizer) {
    let dX = sender.translation(in: view).x
    if sender.state == .ended {
        let fraction = abs(dX / view.bounds.width)
        if fraction >= 0.35 {
            //back navigation code here
        }
    }
}

Don't forget to make the ViewController inherit from the UIGestureRecognizerDelegate class (i.e. class WebViewController: UIViewController, WKUIDelegate, WKNavigationDelegate, UIGestureRecognizerDelegate). The delegate in question here is UIGestureRecognizerDelegate but I anticipate you'll need the others as well.