【问题标题】:how to store data to local device from server with titanium?如何使用钛将数据从服务器存储到本地设备?
【发布时间】:2015-02-07 02:42:23
【问题描述】:
我想在用户第一次使用APP时进行自动登录。我尝试将其保存在alloy.js和Ti.App.Properties.getString('login_token')中,但它们不起作用。
在我的咖啡里:
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
【问题讨论】:
标签:
cookies
titanium
titanium-mobile
titanium-alloy
titanium-modules
【解决方案1】:
我找不到执行此操作的内置方法,因此我只是创建了一个 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;
}