Cordova - window.history.back()不支持iOS 9中的HTML后退按钮

sou*_*mya 6 html javascript cordova ios9 cordova-3.8.0

在我的应用程序中,我使用window.history.back导航回上一个View

后退按钮声明

 <div class="back_icon"  id="verification_back_icon"><a href="#" data-rel="back"  data-transition="slidedown"><img src="images/back_btn.png" width="23"/></a></div>
Run Code Online (Sandbox Code Playgroud)

按钮动作:

$( "#verification_back_icon" ).on( "click", function ( e ) {
    if ( checkDirtyVacation() ) {
        e.preventDefault();
        if ( backbtnAlt == false ) {
            backbtnAlt = true;
            confirm( "All data will be lost. Do you want to continue?",
                function ( r ) {
                    if ( r ) {
                        //onBackKeyDown();
                        clearVacationvalues();
                        window.history.back();//this is not working in iOS 9
                    } else {

                    }
                    backbtnAlt = false;
                } );
        }
    }
    else {
        e.preventDefault();
        if ( $( ".vaction_location" ).hasClass( "chkSelect" ) ) {
            $( ".vaction_location" ).removeClass( "chkSelect" );
            $( ".vaction_location" ).addClass( "chkUnSelect" );
        }


        window.history.back();
    }
} );
Run Code Online (Sandbox Code Playgroud)

这完美地工作到iOS 8.4.在iOS 9中,此导航无效.

我在用Apache Cordova native platform version 3.8.0.

如果有人遇到类似的问题请建议我.我试过使用history.back在使用Cordova的iOS上不起作用,但没有运气

谢谢.

Dav*_*den 6

问题是window.location.hash在iOS 9.0 UIWebview 中设置是异步的(由Cordova/Phonegap使用) - 有关详细信息,请参阅此错误报告.

这在使用jQuery Mobile时会导致问题,默认情况下,jQuery Mobile用于window.location.hash在"页面"之间导航.它还会导致使用此机制的弹出窗口/对话框/选择菜单出现问题.

您可以通过阻止jQuery Mobile自动侦听/使用location.hash来解决此问题:

$(document).on("deviceready", function(){
    $.mobile.hashListeningEnabled = false;
});
Run Code Online (Sandbox Code Playgroud)

但是,我发现这对Android有副作用,例如导致硬件后退按钮无法工作,所以我使用cordova-plugin-device专门针对iOS 9进行了定位:

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

请注意,我navigator.app.backHistory()没有window.history.back()使用hashListeningEnabled = false- 这可能会有所不同.

或者,您可以使用此插件在iOS 8和9上使用新的WKWebView.在iOS 8+上,Safari使用WKWebView,因此在iOS 9的浏览器中查看的JQM站点不会遇到这些问题.由于iOS 8中WKWebView的一个错误,cordova-ios 3仍然使用UIWebView ,但即将推出的cordova-ios 4 将支持 iOS 9+ 的WKWebView核心插件.请注意,将WKWebView与Cordova/Phonegap应用程序一起使用时还有其他注意事项,因为它具有更严格的安全性,例如在XHR响应中需要CORS标头.


sou*_*mya 2

解决方案:

此行解决了我的问题:

 history.go(0); 
Run Code Online (Sandbox Code Playgroud)

我已经替换 window.history.back()history.go(0);

现在它在 iOS 9 中对我来说工作得很好

在index.html中

   <script type="text/javascript">$.mobile.hashListeningEnabled = false;</script>
Run Code Online (Sandbox Code Playgroud)

在 onDeviceReady 函数中添加以下内容:

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

    if ( !( $.mobile.hashListeningEnabled &&
        $.mobile.path.isHashValid( location.hash ) &&
        ( $( hashPage ).is( ":jqmData(role='page')" ) ||
            $.mobile.path.isPath( hash ) ||
            hash === $.mobile.dialogHashKey ) ) ) {

        // make sure to set initial popstate state if it exists
        // so that navigation back to the initial page works properly
        if ( $.event.special.navigate.isPushStateEnabled() ) {
            $.mobile.navigate.navigator.squash( path.parseLocation().href );
        }

        $.mobile.changePage( $.mobile.firstPage, {
            transition: "none",
            reverse: true,
            changeHash: false,
            fromHashChange: true
        } );
    } else {
        // trigger hashchange or navigate to squash and record the correct
        // history entry for an initial hash path
        if ( !$.event.special.navigate.isPushStateEnabled() ) {
            $window.trigger( "hashchange", [true] );
        } else {
            // TODO figure out how to simplify this interaction with the initial history entry
            // at the bottom js/navigate/navigate.js
            $.mobile.navigate.history.stack = [];
            $.mobile.navigate( $.mobile.path.isPath( location.hash ) ? location.hash : location.href );
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

验证设备操作系统版本(仅适用于 iOS 9) 在 iOS 9 版本完美运行history.go(0)之前 window.history.back()

现在添加这段代码代替 window.history.back()

if ( device.platform === "iOS" && parseInt( device.version ) === 9 ) {
    console.log( "version" + device.version );
    console.log( "iOS 9" );
    history.go( 0 );
    //write your code here                 
}
else {
    window.history.back();
}
Run Code Online (Sandbox Code Playgroud)

要修复控制台中的此消息“无法加载网页并出现错误:CDVWebViewDelegate:导航在 state=1 时启动”,请在CDVWebViewDelegate.m中添加以下代码

在 - (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)请求 navigationType:(UIWebViewNavigationType)navigationType

方法注释这一段代码如下所示:

/*if ([_delegate respondsToSelector:@selector(webView:didFailLoadWithError:)]) {
    NSDictionary* errorDictionary = @{NSLocalizedDescriptionKey : description};
    NSError* error = [[NSError alloc] initWithDomain:@"CDVWebViewDelegate" code:1 userInfo:errorDictionary];
    [_delegate webView:webView didFailLoadWithError:error];
}*/
Run Code Online (Sandbox Code Playgroud)