Phone GAP SessionStorage
I working on an iPhon开发者_JS百科e Application using phone GAP.In my app we are using an external DB .User login using web service, i need to store user ID after login .How i store the user ID using phone GAP.can i use phone GAP Session Storage for this?is possible?
Any one knows please helps.
Thanks, Companion.
You really don't have the concept of "session" in Phonegap - you have HTML5 localStorage to store persistent data (think "application scope"):
var userId = localStorage.getItem("userId");
if (userId==null || userId==0) {
jQT.goTo("#login");
}
Log user in:
$('#btnLogin').click(function(){
$("#loginFailure").hide();
$.getJSON(svcUri + "authenticate.cfm?username="+$("#username").val()+"&password="+$("#password").val() + "&callback=?",function(data) {
localStorage.setItem("userId",data.userid);
userId = data.userid;
if (data.userid != 0) {
// do some tasks after logging in
jQT.goTo('#travelz');
} else {
$("#loginFailure").show();
}
});
return false;
});
Lawnchair is probably overkill just to store and ID, just use HTML5 local storage.
You could try lawnchair to store data as JSON.
There is a concept of SessionStorage. It works the same way as localStorage, but gets erased every time you close the application
var keyName = window.sessionStorage.key(0); //Get key name
window.sessionStorage.setItem("key", "value"); //Set item
var value = window.sessionStorage.getItem("key");// Get item
window.sessionStorage.removeItem("key"); //Remove Item
window.sessionStorage.clear();//Clear storage
You can set session storage like that
var userid = 10;
sessionStorage.setItem('UserId',userid);
You will get this session variable like that
var data = sessionStorage.getItem('UserId');
Note: This variable will reset after close app but if you wants to save on localstorage then you need localStorage function which will not reset after close app
精彩评论