简体   繁体   English

history.back在使用Cordova的iOS上不起作用

[英]history.back doesn't work on iOS using Cordova

I have a hybrid application developed with Cordova. 我有一个与Cordova一起开发的混合应用程序。 The app is very simple, so I haven't used a framework. 该应用程序非常简单,所以我没有使用框架。 Almost all the pages are injected via Ajax using the jQuery's ajax() method, and then added in the history using the HTML5 History API via the pushState() method. 几乎所有页面都是使用jQuery的ajax()方法通过Ajax注入的,然后通过pushState()方法使用HTML5 History API添加到历史记录中。

To allow the user to go back to a previously visited page (a page in the history), I've created a button. 为了让用户返回之前访问过的页面(历史记录中的页面),我创建了一个按钮。 I listen for the backbutton event as well as taps on that button and when the event is fired, I execute the following handler: 我监听backbutton按钮事件以及点击该按钮,当事件被触发时,我执行以下处理程序:

onBackButton: function() {
   window.history.length === 0 ? navigator.app.exitApp() : window.history.back();
}

To be sure to have the maximum compatibility possible, I've also added history.js to the project. 为了确保最大程度的兼容性,我还将history.js添加到项目中。

It works like a charm on Android and on the iOS simulator on the Mac, but doesn't work on a real device. 它就像Android上的魅力和Mac上的iOS模拟器一样,但不适用于真实设备。 What happens is that iOS catch the event but the execution of the method doesn't change the page. 会发生什么是iOS捕获事件但该方法的执行不会更改页面。 I've already tried several methods based on previous answers on StackOverflow, but no luck. 我已经基于StackOverflow上的先前答案尝试了几种方法,但没有运气。 Some examples are: history.back() not working in phonegap ios build and Phonegap - navigator.app.backHistory() not working on HTML back button . 一些例子是: history.back()不能在phonegap ios构建中工作Phonegap - navigator.app.backHistory()不能处理HTML后退按钮

What I can try? 我能尝试什么?

You can simply use, 你可以简单地使用,

history.go(-1);

to navigate back. 导航回来。

Try this one. 试试这个吧。

main.js main.js

var pagesHistory = [];
var currentPage = {};
var path = "";

page1.js page1.js

currentPage = {};
currentPage.back = function() {
    pagesHistory.pop();
};
currentPage.loadPage2 = function() {
    pagesHistory.push(path + "pages/Page2.html");
};

page2.js page2.js

currentPage = {};
currentPage.back = function() {
    pagesHistory.pop();
};
currentPage.loadPage1 = function() {
    pagesHistory.push(path + "pages/Page1.html");
};

Reference: http://public.dhe.ibm.com/software/mobile-solutions/worklight/docs/v620/BuildingMultiPageApplicationProject.zip 参考: http//public.dhe.ibm.com/software/mobile-solutions/worklight/docs/v620/BuildingMultiPageApplicationProject.zip

Seen this answer from a few months back? 从几个月前看到这个答案? Apparently iOS has some security issues when it comes to navigating history through script alone. 显然,iOS仅通过脚本导航历史记录时会遇到一些安全问题。 The answer here also includes a work around by hashing the URLs: StackOverflow Question 这里的答案还包括通过散列URL来解决: StackOverflow问题

Dont know if cordova itself has the navigator.. but I usually used this function for going back: 不知道cordova本身是否有导航器..但我通常使用此功能返回:

function goBack() {
    //or history.back()
    history.go(-1);
    navigator.app.backHistory();
}

Could not test it though. 虽然无法测试它。

Edit: Since you don't do full refresh and load almost everything through ajax, you maybe have to implement the history back yourself.. the iphone has some problems with javascript only pages when going back. 编辑:由于你没有完全刷新并通过ajax加载几乎所有东西,你可能必须自己实现历史记录... iphone在返回时只有javascript页面有一些问题。 Since you push your actions to the history it shouldn't be to hard: 既然你将你的行动推向了历史,那就不应该变得困难:

window.addEventListener("popstate", function(e) {
    //get it from the history, execute your functions to show the page as usual
});

app.application.navigate("#:back")怎么样?

I don't use the Cordova, but this code might help you: 我不使用Cordova,但此代码可能对您有所帮助:

String . prototype . isEmpty = function () { return this . length === 0; };

var browserHistory;

function hash ( name, historyChange )
{
    "use strict";
    if ( name !== undefined )
    {
        if ( hash () !== name )
        {
            if ( ! historyChange ) { browserHistory . add ( name ); }
            window . location . hash = name;
        }
    }
    else
    {
        var newHash = window . location . hash . split ( "#" ) [ 1 ];
        return newHash === undefined ? "" : newHash;
    }
}

browserHistory =
{
    currentIndex : 0,
    history : [],
    add : function ( name )
    {
        "use strict";
        this . currentIndex = this . currentIndex + 1;
        if ( this . history . length - 1 > this . currentIndex ) { this . history . splice ( this . currentIndex ); }
        this . history . push ( name );
    },
    backward : function ()
    {
        "use strict";
        if ( this . currentIndex === 0 ) { return; }
        this . currentIndex = this . currentIndex - 1;
        this . changeHash ();
    },
    changeHash : function ()
    {
        "use strict";
        hash ( this . history [ this . currentIndex ], true );
    },
    forward : function ()
    {
        "use strict";
        if ( this . currentIndex === this . history . length - 1 ) { return; }
        this . currentIndex = this . currentIndex + 1;
        this . changeHash ();
    },
    init : function ( name )
    {
        "use strict";
        this . history . push ( name );
    }
};

window . onhashchange = function ()
{
    "use strict";
    if ( hash () . isEmpty () ) { load ( { name: "" } ); }
    else { load ( { name: hash () } ); }
};

window . onload = function ()
{
    "use strict";
    if ( ! hash () . isEmpty () )
    {
        browserHistory . init ( hash () );
        load ( { name: hash () } ); 
    }
    else { browserHistory . init ( "" ); }
};

This script: 这个脚本:
1) Work with sites that uses hashes ( http(s)://(www.)name.domain/#urlHash ). 1)使用使用哈希的网站(http(s)://(www。)name.domain /#urlHash)。

Examle:
https://www.example.com/page - default url
https://www.example.com/page#login - login subpage
https://www.example.com/page#images - images subpage
etc.

2) "window . onload" function check if entered url contain a hash and init browserHistory with it or with: "" ( empty string ) - no hash is my main page 2)“window.onload”函数检查输入的url是否包含一个hash和init browserHistory,或者带有:“”(空字符串) - 没有哈希是我的主页
3) "browserHistory" object save a page history 3)“browserHistory”对象保存页面历史记录
4) "hash" function called without arguments returns current url hash and with "name" parameter change url hash 4)不带参数调用的“hash”函数返回当前url hash并使用“name”参数更改url hash
5) When hash is changed "window . onhashchange" function is called 5)当改变散列时,调用“window.onhashchange”函数
6) "window . onhashchange" function check hash 6)“window.onhashchange”函数检查哈希
If the url does not contain hash, script load the default page. 如果url不包含hash,则脚本会加载默认页面。
If the url contain a hash, script load a subpage based on hash. 如果url包含哈希,则脚本会根据哈希加载子页面。
7) In function "load" ( not described here ) I have an XMLHttpRequest, that call php file with name argument and set to main div element html code that was returned from that php file. 7)在函数“load”(这里没有描述)中,我有一个XMLHttpRequest,它调用带有name参数的php文件,并设置为从该php文件返回的main div元素html代码。
8) Instead of ( for example ): 8)而不是(例如):

<a class="a_nice_style" href="https://www.example.com/images.html>Images gallery</a>

You can use ( for examle ): 你可以使用(例如):

<p class="a_nice_style" onclick="hash ( 'images' );">Images gallery</p>

9) Function named: "load" have an object as a parameter. 9)名为的函数:“load”有一个对象作为参数。 This object is send to php file ( as an "$_GET" or "$_POST" array ), so you can call "load" function with custom object that contains for example login fields values. 此对象发送到php文件(作为“$ _GET”或“$ _POST”数组),因此您可以使用包含例如登录字段值的自定义对象调用“load”函数。
10) "browserHistory" have only hashes of your page. 10)“browserHistory”只有你页面的哈希值。

This will be your button onclick function: 这将是你的按钮onclick功能:

onBackButton: function() { browserHistory . backward (); }

You can modify a "browserHistory . backward" function to exit your app by replace: "return;" 您可以修改“browserHistory.backward”功能以通过替换退出您的应用:“return;” in this line: 在这一行:

if ( this . currentIndex === 0 ) { return; }

with your application exit code. 与您的应用程序退出代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM