开发者

appcelerator titanium - Ti.App.Properties not working on android

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开发者_如何学Go, 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


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);


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


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();
    }
})();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜