Titanium Properties Get and Set in Best way

Build cross platform apps using PhoneGap, Titanium and many more. Let see with SDK on by one.

PhoneGap :- It is Best solution for creating cross platform apps. Web developers can start creating native apps with out any training. Because we can develop apps using HTML5 and friends (JS and CSS3). It will load HTML pages, CSS and Javascripts file from resource folder to WebBrowser control.

Titanium :- Another Great solution for cross platform apps. Same like PhoneGap, here also we can using JavaScript. But, it will create native UI apps. So it will be preforms very fast. Please check with API document http://bit.ly/TiDoc

Appcelerator Titanium
Appcelerator Titanium

To save App settings Titanium have great solution, There is module called Titanium.App.Properties .

This is Module is used for storing application-related data in property/value pairs that persist beyond application sessions and device power cycles.

To set property:
Ti.App.Properties.setString('website', 'https://shareourideas.com/');
To get property
Ti.App.Properties.getString('website'); //Here we have to remember one thing, there is optional parameter for this get string. It is Default value.

Let check when we can take advantage of default value.

For example:-
var twName = Ti.App.Properties.getString('twitterName');
if(twName.length != 0 ){alert("Twitter user name "+ twname);}

In the above code there twitterName property is not set. So, twName will be null and when we are trying to find the length of the string. it will through an exception.
Before getString we can also using check for property exiting using this hasProperty method. it will return Boolean value.
So, It will be look like this.

if(Ti.App.Properties.hasProperty('twitterName'))
{
var twName = Ti.App.Properties.getString('twitterName');
if(twName.length != 0 ){alert("Twitter user name "+ twname);}
}

We can also using default value to getString

var twName = Ti.App.Properties.getString('twitterName',' did not set yet! '); //passing Default value as a parameter
if(twName.length != 0 ){alert("Twitter user name "+ twname);}

If there is no twitterName it will return default value. And remember one thing we always check with property name is having twitterName or whatever. So, try to trim the string in Javascript we did not have trim method But we can using replace method “.replace(/^ss*/, '').replace(/ss*$/, '')“.

Leave a Reply