Ionic hybrid app, saving application state/data on

2019-06-01 08:38发布

I'm building an app using the ionic framework with angular js, and I'm a little confused on what the best way is to go about saving and restoring the users state when they exit the app. For example, in iOS if the user manually terminates the app, or if the phone is turned off for some reason, I want the user to be able to open my app up and still be at the same place and see the same data, even if they are offline.

I have been using HTML localstorage, and understand how to save and restore the users data locally, and similarly I understand how to save a reference to their current state. When the user is actually using the app however I'm gonna need to be pulling and pushing data from a cloud database, so I would only really use the local storage to save the users state when they exit.

What is the best way to go about saving their state in angular? When should I do it, and is their a way to tell if and when the app is terminating. Ideally I would like to be able to call a function to save the state right before the app terminates, however I have been unable to find the right way to do this in angular.

As this is a fairly specific issue, Im hoping to learn to right way to do it. Thanks!!

What Im looking for

// P S U D O    C O D E 
on('application_exit', function(){
    // save user data
})

1条回答
Luminary・发光体
2楼-- · 2019-06-01 09:08

You can use the Cordova "pause" event, which is triggered whenever your app is sent to the background.

document.addEventListener("pause", yourCallbackFunction, false);

http://docs.phonegap.com/en/2.9.0/cordova_events_events.md.html#pause

You'll want to look at the iOS quirks to make sure they fit your use case, however.

查看更多
登录 后发表回答