문제

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.

도움이 되었습니까?

해결책

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top