2011-01-05 37 views
6

我在使用手機GAP的iPhone應用程序工作。在我的應用程序中,我們使用的是外部數據庫。使用Web服務的用戶登錄,我需要在登錄後存儲用戶ID。如何使用手機GAP存儲用戶ID 。我可以使用手機GAP會話存儲嗎?可以嗎?Phone GAP SessionStorage

任何人都知道請幫助。

謝謝, 同伴。

回答

12

你真的沒有在PhoneGap的 「會話」 的概念 - 你必須HTML5的localStorage存儲持久性數據(認爲 「適用範圍」):

var userId = localStorage.getItem("userId"); 
if (userId==null || userId==0) { 
    jQT.goTo("#login"); 
} 

登錄用戶:

$('#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; 

});

+0

使用GET請求不是登錄的方式。它太脆弱了。改爲使用POST請求,通過SSL來保證安全。 – baklap 2013-02-01 06:59:15

+0

「你真的沒有Phonegap中的」會話「的概念」 - 這是不正確的 – 2013-12-18 05:08:07

3

您可以嘗試lawnchair將數據存儲爲JSON。

+0

這方面的好處是,您可以輕鬆地將其移植到其他PhoneGap平臺,而這些平臺不一定支持特定的脫機存儲機制。 Kris的建議是正確的(我贊成它!),但更強大的解決方案將是使用Lawnchair在未來在其他平臺上使用該應用程序。 – 2011-01-05 22:58:30

+0

lawnchair鏈接斷開 – botbot 2012-12-19 09:40:05

9

Lawnchair可能是矯枉過正的存儲和ID,只需使用HTML5 local storage

0

There is SessionStorage的概念。它的工作方式爲localStorage的相同,但被擦除每次關閉應用程序

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 
0

您可以設置會話存儲一樣,

var userid = 10; 
sessionStorage.setItem('UserId',userid); 

你會得到這樣的

var data = sessionStorage.getItem('UserId'); 
這個會話變量時間

注意:這個變量會在關閉應用程序後重置,但如果您想要保存在localstorage上,那麼您需要localStorage函數,在關閉應用程序後不會重置