appcelerator titan - Ti.App.Properties 不适用于 Android

发布于 2024-11-27 08:35:10 字数 1029 浏览 2 评论 0原文

我正在使用 appcelerator titan 创建一个(iphone/android)移动应用程序。我在使用 Ti.App.Properties 时遇到问题, 我想保存用户的登录数据(用户名和密码),我使用 Ti.App.Properties 的 getList 和 setList 方法在应用程序启动时获取和设置用户名和密码。它在 iPhone 上运行良好,但在 Android 上,应用程序启动时不会检索数据(用户名和密码)。 这是应用程序启动时执行的代码:

var userDataArray=[{title:'name',value:''}, 
                {title:'password',value:''}];
if(Ti.App.Properties.hasProperty("userDataArray"))
{
    userDataArray = Ti.App.Properties.getList("userDataArray");
}
else
{
    Ti.App.Properties.setList("userDataArray",userDataArray);
}
if((Ti.App.Properties.getList("userDataArray")[0].value.length==0)||(Ti.App.Properties.getList("userDataArray")[1].value.length==0))//check if name, password have no values.. on android, this is always the case, which is not correct
{
        //go to login page
}
else if((Ti.App.Properties.getList("userDataArray")[0].value.length>0)&&(Ti.App.Properties.getList("userDataArray")[1].value.length>0))//if both username and password exist
{
        //start
}

谢谢

I am creating an (iphone/android) mobile app using appcelerator titanium. I have a problem using Ti.App.Properties,
I want to save the user's login data (username and password), I used Ti.App.Properties's getList and setList methods to get and set username and password at app startup. It is working fine on iPhone, but on android the data (username and password) are not retrieved at app startup.
here is the code that is executed at app startup :

var userDataArray=[{title:'name',value:''}, 
                {title:'password',value:''}];
if(Ti.App.Properties.hasProperty("userDataArray"))
{
    userDataArray = Ti.App.Properties.getList("userDataArray");
}
else
{
    Ti.App.Properties.setList("userDataArray",userDataArray);
}
if((Ti.App.Properties.getList("userDataArray")[0].value.length==0)||(Ti.App.Properties.getList("userDataArray")[1].value.length==0))//check if name, password have no values.. on android, this is always the case, which is not correct
{
        //go to login page
}
else if((Ti.App.Properties.getList("userDataArray")[0].value.length>0)&&(Ti.App.Properties.getList("userDataArray")[1].value.length>0))//if both username and password exist
{
        //start
}

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

谈情不如逗狗 2024-12-04 08:35:10

我认为你的整体方法是有缺陷的,你不需要一个数组,只需要一张地图

// save the values as a string..
Ti.App.Properties.setString({"username":"myname", "password":"mypassword"}, "CREDENTIALS");

// retrieve the values as a string, but parse it back into an object
var credObject = JSON.parse(Ti.App.Properties.getString("CREDENTIALS"));

// dump the output
Ti.API.debug("Username "+ credObject.username);
Ti.API.debug("Password "+ credObject.password);

i think your overall approach is flawed, you dont need an array just an map

// save the values as a string..
Ti.App.Properties.setString({"username":"myname", "password":"mypassword"}, "CREDENTIALS");

// retrieve the values as a string, but parse it back into an object
var credObject = JSON.parse(Ti.App.Properties.getString("CREDENTIALS"));

// dump the output
Ti.API.debug("Username "+ credObject.username);
Ti.API.debug("Password "+ credObject.password);
简单气质女生网名 2024-12-04 08:35:10

两点说明:

  • .setString() 的参数是相反的,即 Name 和 Value
  • 值必须是字符串,因此您必须对其进行 stringify() 或将其作为字符串输入

two remarks :

  • arguments for .setString() is opposite, ie Name then Value
  • Value must be a string, so you have to stringify() it or enter it as a string
紅太極 2024-12-04 08:35:10

我知道这已经很旧了,但今天仍然具有相关性,因为对钛的帮助并不多。我分两部分处理这个问题。

第 1 部分)在用户的凭据经过身份验证后...

var username = "some username";
var password = "some password";

// Build the object and then convert it to a json string.
oCredentials = new Object();
oCredentials.username = username;
oCredentials.password = password;
var stringCredentials = JSON.stringify(oCredentials);

// Save the credentials
Ti.App.Properties.setString("Credentials", stringCredentials);

第 2 部分)在使用登录窗口/弹出窗口/其他内容提示用户之前...

// Look for credentials
(function() {
    var storedCredentials = Ti.App.Properties.getString("Credentials");

    if (storedCredentials){
        var oJson = JSON.parse(storedCredentials);

        // Call your authentication function
        // For example, autoAuthenticate(oJson.username, oJson.password);
    } else {
        // kick the user out to your login window
        // For example, $.loginWindow.open();
    }
})();

I know this is old, but it's still relevant today as there's not a huge amount of help with Titanium. I handle this in two parts.

Part 1) After the user's credentials have been authenticated...

var username = "some username";
var password = "some password";

// Build the object and then convert it to a json string.
oCredentials = new Object();
oCredentials.username = username;
oCredentials.password = password;
var stringCredentials = JSON.stringify(oCredentials);

// Save the credentials
Ti.App.Properties.setString("Credentials", stringCredentials);

Part 2) Before you prompt the user with the login window/popup/whatever...

// Look for credentials
(function() {
    var storedCredentials = Ti.App.Properties.getString("Credentials");

    if (storedCredentials){
        var oJson = JSON.parse(storedCredentials);

        // Call your authentication function
        // For example, autoAuthenticate(oJson.username, oJson.password);
    } else {
        // kick the user out to your login window
        // For example, $.loginWindow.open();
    }
})();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文