Phone GAP SessionStorage

你离开我真会死。 提交于 2020-01-12 19:04:40

问题


I working on an iPhone 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.


回答1:


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;

});




回答2:


Lawnchair is probably overkill just to store and ID, just use HTML5 local storage.




回答3:


You could try lawnchair to store data as JSON.




回答4:


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



回答5:


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



来源:https://stackoverflow.com/questions/4601174/phone-gap-sessionstorage

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!