Phone GAP SessionStorage

2020-07-10 10:31发布

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.

5条回答
我命由我不由天
2楼-- · 2020-07-10 10:51

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

查看更多
干净又极端
3楼-- · 2020-07-10 10:58

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

查看更多
叼着烟拽天下
4楼-- · 2020-07-10 11:02

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;

});

查看更多
别忘想泡老子
5楼-- · 2020-07-10 11:12

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
查看更多
淡お忘
6楼-- · 2020-07-10 11:17

You could try lawnchair to store data as JSON.

查看更多
登录 后发表回答