8

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?

Eric Aya
  • 68,765
  • 33
  • 165
  • 232

4 Answers4

16

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()
        }
    }
}
Gabriel Madruga
  • 181
  • 1
  • 5
9

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

OD Applications
  • 115
  • 1
  • 10
3

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");   
} 

}
mKane
  • 854
  • 11
  • 28
  • Thank you for your answer. Where to put this code? Under [super viewDidLoad]; in ViewController.m? –  Aug 14 '15 at 14:39
  • yes you can add this in your viewdidload. Your welcome. If it works be sure to accept the answer:) – mKane Aug 14 '15 at 14:45
  • [imageView addGestureRecognizer:swipeLeft]; What is imageView? I've a WebView. –  Aug 14 '15 at 14:47
  • 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?) –  Aug 14 '15 at 16:14
  • Handle swipe is the method it invokes where you would add the back forward actions for your web view, and yes that's right – mKane Aug 14 '15 at 19:22
  • Please see my "answer" below! –  Aug 14 '15 at 20:06
2

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)
  }
}
Suyog
  • 133
  • 11