Swipe gesture for back/forward in UIWebView?
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:
- Drag and drop a WebView into the storyboard. Apply required constraints.
- Declare the IBOutlet of the WebView. I named it webViewBox.
- Import WebKit framework. (import WebKit)
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)
}
}
Admin
Updated on July 27, 2022Comments
-
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 about 8 yearsThank you for your answer. Where to put this code? Under [super viewDidLoad]; in ViewController.m?
-
mKane about 8 yearsyes you can add this in your viewdidload. Your welcome. If it works be sure to accept the answer:)
-
Admin about 8 years[imageView addGestureRecognizer:swipeLeft]; What is imageView? I've a WebView.
-
Admin about 8 yearsI 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 about 8 yearsHandle swipe is the method it invokes where you would add the back forward actions for your web view, and yes that's right
-
Admin about 8 yearsPlease see my "answer" below!
-
marq over 5 yearsJust to clarify,
WKWebView
has theallowsBackForwardNavigationGestures
property whileUIWebView
does not. -
htafoya almost 4 yearsThis is kind of sensitive, user could do accidental swipes
-
Suyog almost 4 yearsYes @htafoya, but these gestures are kind of universal now. Its not that easy for someone to accidentally do so