Cookies 如何使用钛合金将数据从服务器存储到本地设备?

Cookies 如何使用钛合金将数据从服务器存储到本地设备?,cookies,titanium,titanium-mobile,titanium-alloy,titanium-modules,Cookies,Titanium,Titanium Mobile,Titanium Alloy,Titanium Modules,我想在用户第一次使用应用程序后自动登录。我试图将其保存在alloy.js和Ti.APP.Properties.getString(“登录令牌”)中,但都不起作用 在我的咖啡里: result = JSON.parse this.responseText console.info result.token #"dsfdsfds2142fds3r32rf32e3dfefwedf" Ti.App.Properties.setString "token",result.token console.in

我想在用户第一次使用应用程序后自动登录。我试图将其保存在alloy.js和Ti.APP.Properties.getString(“登录令牌”)中,但都不起作用

在我的咖啡里:

result = JSON.parse this.responseText
console.info result.token  #"dsfdsfds2142fds3r32rf32e3dfefwedf"
Ti.App.Properties.setString "token",result.token
console.info Ti.App.Properties.getString "token"  # it's blank

我找不到一种内置的方法来实现这一点,所以我只是创建了一个getter和setter方法,并将其放在alloy.js中。感觉非常粗糙和肮脏,但它确实有效

//retrieves the value of the token
// also checks if the token is valid and sets logged_in accordingly
function getToken(){
    var f = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory,'api.txt');
    var content = f.read();

    //if we can read this return it, otherwise return a blank value
    if (content){
      return content.text;  
    }
    else {
        return '';
    }
}

//persists and sets the value of token to the new value
function setToken(key){
    var f = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory,'api.txt');
    f.deleteFile();
    var f = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory,'api.txt');
    f.write(key);
    token = key;
}

粘贴完整的代码,让我知道你需要做什么详细?