Swipe gesture for back/forward in UIWebView?
Asked Answered
I

4

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?

Illuminance answered 14/8, 2015 at 14:26 Comment(0)
T
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");   
} 

}
Tetragrammaton answered 14/8, 2015 at 14:37 Comment(6)
Thank you for your answer. Where to put this code? Under [super viewDidLoad]; in ViewController.m?Illuminance
yes you can add this in your viewdidload. Your welcome. If it works be sure to accept the answer:)Tetragrammaton
[imageView addGestureRecognizer:swipeLeft]; What is imageView? I've a WebView.Illuminance
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?)Illuminance
Handle swipe is the method it invokes where you would add the back forward actions for your web view, and yes that's rightTetragrammaton
Please see my "answer" below!Illuminance
H
17

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()
        }
    }
}
Himelman answered 28/3, 2017 at 20:12 Comment(0)
S
13

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

Saulsauls answered 19/2, 2018 at 20:54 Comment(1)
Just to clarify, WKWebView has the allowsBackForwardNavigationGestures property while UIWebView does not.Emptor
T
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");   
} 

}
Tetragrammaton answered 14/8, 2015 at 14:37 Comment(6)
Thank you for your answer. Where to put this code? Under [super viewDidLoad]; in ViewController.m?Illuminance
yes you can add this in your viewdidload. Your welcome. If it works be sure to accept the answer:)Tetragrammaton
[imageView addGestureRecognizer:swipeLeft]; What is imageView? I've a WebView.Illuminance
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?)Illuminance
Handle swipe is the method it invokes where you would add the back forward actions for your web view, and yes that's rightTetragrammaton
Please see my "answer" below!Illuminance
F
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)
  }
}
Faitour answered 9/8, 2019 at 13:8 Comment(2)
This is kind of sensitive, user could do accidental swipesManganate
Yes @htafoya, but these gestures are kind of universal now. Its not that easy for someone to accidentally do soFaitour

© 2022 - 2024 — McMap. All rights reserved.