Cordova - window.history.back() not working on HTML back button in iOS 9

前端 未结 6 1562
失恋的感觉
失恋的感觉 2020-12-05 21:48

In my application I am using window.history.back to navigate back to previous View

Declaration of back button

 <         


        
6条回答
  •  时光取名叫无心
    2020-12-05 22:24

    The problem is that setting of window.location.hash is asynchronous in the iOS 9.0 UIWebview (used by Cordova/Phonegap) - see this bug report for details.

    This causes issues when using jQuery Mobile which by default uses window.location.hash to navigate between "pages". It also causes issues with popups/dialogs/select menus which use this mechanism.

    You can fix this by preventing jQuery Mobile from automatically listening/using location.hash:

    $(document).on("deviceready", function(){
        $.mobile.hashListeningEnabled = false;
    });
    

    However, I found this had side effects on Android such as causing the hardware back button not to work, so I targeted it specifically at iOS 9 using cordova-plugin-device:

    $(document).on("deviceready", function(){
        if(device.platform === "iOS" && parseInt(device.version) === 9){
            $.mobile.hashListeningEnabled = false;
        }
    });
    

    Note that I'm using navigator.app.backHistory() not window.history.back() in conjunction with hashListeningEnabled = false - this may make a difference.

    Alternatively you can use this plugin to use the new WKWebView on iOS 8 and 9. WKWebView is used by Safari on iOS 8+, hence JQM sites viewed in the browser on iOS 9 don't encounter these issues. cordova-ios 3 still uses UIWebView due to a bug in WKWebView in iOS 8, but the upcoming cordova-ios 4 will support a WKWebView core plugin for iOS 9+. Note that there are additional considerations when using WKWebView with Cordova/Phonegap apps due to its stricter security, such as requiring CORS headers on XHR responses.

提交回复
热议问题