cordova-plugin-inappbrowser
cordova-plugin-inappbrowser copied to clipboard
[iOS] WKWebview - Back button in location bar is always enabled
Issue Type
- [x] Bug Report
Description
Click on back button of location bar or history.back() or history.go(-1), the back button of location bar is always enabled. User can click on back button or call history.back() or history.go(-1), with no endpoint.
It looks like the back action always pushes new URL to history stack instead going back
Information
I'm using usewkwebview=yes. When open inappbrowser in the first time, back button is disabled. But when click on some URL, the back button is always enabled.
I checked this function in CDVWKInAppBrowser.m
- (void)webView:(WKWebView *)theWebView didFinishNavigation:(WKNavigation *)navigation
theWebView.canGoBack; is always returns true after the first redirect
Command or Code
const target = '_blank';
const options = [
'location=yes',
'hidden=no',
'hideurlbar=no',
'toolbar=yes',
'hidenavigationbuttons=no',
'zoom=no',
'transitionstyle=crossdissolve',
'enableViewportScale=yes',
'usewkwebview=yes',
'beforeload=get'
];
inAppBrowserRef = window.cordova.InAppBrowser.open(url, target, options.join());
Environment, Platform, Device
Cordova ver 9
Platform: iOS 13, Device: iPhone 7
package.json cordova-plugin-inappbrowser: "3.1.0" cordova-plugin-wkwebview-engine: "1.2.1"
Checklist
- [x] I searched for already existing GitHub issues about this
- [x] I updated all Cordova tooling to their most recent version
- [x] I included all the necessary information above
I noticed the same behavior in my app, and I think it's a side-effect of using beforeload mechanism. When this is enabled, all navigations are initially cancelled and the beforeload event is triggered instead. When the JS event handler calls the callback parameter to allow the navigation, this actually performs a regular navigation in the webview, pushing the target URL in the history stack. The navigation buttons are useless when beforeload is enabled.
Is there any solutions available to resolve this issue?
I don't think there is any. It's intrinsic to the way beforeload is working.
I did a workaround by manually calculating the number of times to load new url and back history.
Ex:
- when you open inappbrowser first time, set the countFlag = 0
- listen the event
historyGoTowhen redirect to new url => countFlag = countFlag + 1 - listen the event
historyGoBack=> countFlag = countFlag - 1 - if countFlag === 0 mean we stand in the first page. If trigger goback, then close the inappbrowser
Note: I dispatch the event historyGoTo and historyGoBack inside the native objective-C code side, then listen them in javascript side for some later purposes.
Hi @thachhn,
Thanks for the swift response. Is it possible for you to share the code here. So will be useful for those having similar issue.
Sorry, it's 3 years from that time. I'm not working on that project anymore so I don't have the code here. But the idea is what I said in the previous comment. You have to clone the project and edit the code. I only remember that I edited 2 files:
-
CDVWKInAppBrowser.m find the function that is called when redirect or go back is called
-
inappbrowser.js define new events are fired here