如何从钛服务器将数据存储到本地设备?

时间:2015-02-07 02:42:24

标签: cookies titanium titanium-mobile titanium-alloy titanium-modules

我希望在用户第一次使用APP后进行自动登录。我试图将它保存在alloy.js和Ti.App.Properties.getString(' login_token')中,但是他们没有&# 39;工作。

在我的咖啡中:

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

1 个答案:

答案 0 :(得分:0)

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

//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;
}