Swipe gesture for back/forward in UIWebView?

13,371

Solution 1

Why not just use a swipe gesture recognizer?

UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];

// Setting the swipe direction.
[swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
[swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];

// Adding the swipe gesture on WebView
[webView addGestureRecognizer:swipeLeft];
[webView addGestureRecognizer:swipeRight];

- (void)handleSwipe:(UISwipeGestureRecognizer *)swipe {

if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) {
    NSLog(@"Left Swipe");
}

if (swipe.direction == UISwipeGestureRecognizerDirectionRight) {
    NSLog(@"Right Swipe");   
} 

}

Solution 2

Accepted answer in Swift 3:

override func viewDidLoad() {
    super.viewDidLoad()

    let swipeLeftRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipe(recognizer:)))
    let swipeRightRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipe(recognizer:)))
    swipeLeftRecognizer.direction = .left
    swipeRightRecognizer.direction = .right

    webView.addGestureRecognizer(swipeLeftRecognizer)
    webView.addGestureRecognizer(swipeRightRecognizer)
}

@objc private func handleSwipe(recognizer: UISwipeGestureRecognizer) {
    if (recognizer.direction == .left) {
        if webView.canGoForward {
            webView.goForward()
        }
    }

    if (recognizer.direction == .right) {
        if webView.canGoBack {
            webView.goBack()
        }
    }
}

Solution 3

Answer in Swift 3 & Swift 4

If anyone is still having problems. This worked for me:

Find "didFinish" add / replace the following code.

func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {

    self.didFinish()

    webView.allowsBackForwardNavigationGestures = true

}

The main code you need is just one line. It comes after self.didFinish() but still within the {} brackets.

webView.allowsBackForwardNavigationGestures = true

Solution 4

Gabriel Madruga's answer worked for me. I reduced the lines of code further by:

  1. Drag and drop a WebView into the storyboard. Apply required constraints.
  2. Declare the IBOutlet of the WebView. I named it webViewBox.
  3. Import WebKit framework. (import WebKit)
  4. Add the following lines of code in viewDidLoad()

    let leftSwipe = UISwipeGestureRecognizer(target: webViewBox, action: #selector(webViewBox.goForward))
    leftSwipe.direction = .left
    webViewBox.addGestureRecognizer(leftSwipe)
    
    let rightSwipe = UISwipeGestureRecognizer(target: webViewBox, action: #selector(webViewBox.goBack))
    leftSwipe.direction = .right
    webViewBox.addGestureRecognizer(rightSwipe)
    

Your final result should look like this:

    import UIKit
    import WebKit

    class ViewController: UIViewController {


@IBOutlet weak var webViewBox: WKWebView!

override func viewDidLoad() {
    super.viewDidLoad()


    //URL request:
    let urlReq = URLRequest(url: URL(string: "https://www.google.co.in")!)
    //Load the URL:        
    webViewBox.load(urlReq)



    //To go forward:
    let leftSwipe = UISwipeGestureRecognizer(target: webViewBox, action: #selector(webViewBox.goForward))
    leftSwipe.direction = .left
    webViewBox.addGestureRecognizer(leftSwipe)

    //To go back:
    let rightSwipe = UISwipeGestureRecognizer(target: webViewBox, action: #selector(webViewBox.goBack))
    rightSwipe.direction = .right
    webViewBox.addGestureRecognizer(rightSwipe)
  }
}
Share:
13,371
Admin
Author by

Admin

Updated on July 27, 2022

Comments

  • Admin
    Admin over 1 year

    I have a WebView in my app.

    Because it is a tabbed application I'm not able to add buttons for going back/forward on the website.

    I want to go back/forward by swiping. Right swipe from the left side/edge is back… like in Safari browser for iOS.

    How can I do it? I think i should use "Screen Edge Pan Gesture Recognizer", right?

  • Admin
    Admin about 8 years
    Thank you for your answer. Where to put this code? Under [super viewDidLoad]; in ViewController.m?
  • mKane
    mKane about 8 years
    yes you can add this in your viewdidload. Your welcome. If it works be sure to accept the answer:)
  • Admin
    Admin about 8 years
    [imageView addGestureRecognizer:swipeLeft]; What is imageView? I've a WebView.
  • Admin
    Admin about 8 years
    I get "Use of undeclared identifier 'handleSwipe'". What is handleSwipe? What can I co now? No code in ViewController.h needed? (I added "Swipe Gesture Recognizer" to the WebView. That's right?)
  • mKane
    mKane about 8 years
    Handle swipe is the method it invokes where you would add the back forward actions for your web view, and yes that's right
  • Admin
    Admin about 8 years
    Please see my "answer" below!
  • marq
    marq over 5 years
    Just to clarify, WKWebView has the allowsBackForwardNavigationGestures property while UIWebView does not.
  • htafoya
    htafoya almost 4 years
    This is kind of sensitive, user could do accidental swipes
  • Suyog
    Suyog almost 4 years
    Yes @htafoya, but these gestures are kind of universal now. Its not that easy for someone to accidentally do so