In this chapter, we'll dig deep into the
Ti.App.Properties API. You'll learn how to store both simple and complex data in app properties and retrieve that data later.
Both iOS and Android store app properties in special files on the filesystem. Natively, iOS properties are known as
NSUserDefaults, which are stored in
.plist files in the application's library directory. Android stores them in standard xml text files at
/data/data/com.domainname.appname/shared_prefs/titanium.xml. Titanium provides a uniform means to set and get app properties via the
Reading and Writing Properties
Titanium.App.Properties has six sets of get/set methods for handling six different data types:
- getBool() / setBool(): for booleans (true, false)
- getDouble() / setDouble(): for double-precision floating point numbers
- getInt() / setInt(): for integers
- getList() / setList(): for arrays
- getString() / setString(): for strings
The get methods accept a property name and its default value. Thus, if a property has never been set before, the default value will be returned. Each set method requires a property name and property value pair. All of these methods are demonstrated below:
This code outputs the following results:
Storing JS objects as JSON in properties
JSON.stringify() provided by Titanium.JSON, which will allow you to store it in the database using the Titanium.App.Properties.setString() method.
This will output the following to the log:
In this activity, you will write an app that prompts the user to select temperature units in either Celsius or Fahrenheit and then save that preference in an application property. When the app is loaded, the property should be read and used to update the UI accordingly. When complete, your app should look like the following:
- Create a new Titanium Mobile project named
localdatawith an app ID of
- In app.js, follow our recommended coding best-practices to create a two-tabbed application with tabs titled Current Conditions and Units. You can omit the tab icons.
- Define and assign a window to the Current Conditions tab, but you can leave that window empty. We'll use it in the 5.3 lab that follows.
- Define and assign a window to the Units tab with the following characteristics:
Define and add a label with these properties:
Define and add a "sub" label with these properties:
- Define and add a switch positioned at
autowidth. Its value should be a zero or one depending on the value of the
unitsapp property. If that property equals
c, set the switch value to
0, otherwise set it to
- Add an event handler to the switch: if the switch value is
1, update the sub-label text to read 'Output will be shown in Fahrenheit' and set the
unitsapp property equal to
f. If the value of the switch is
0, update the sub-label text to read 'Output will be shown in Celsius' and set the
unitsapp property equal to
- Build your app for the simulator/emulator. The output should match the preceding picture. When you change the switch, the label should update. Close the app. When you re-open it, your preference should be saved and reloaded.
References and Further Reading
In this chapter, we dug into the
Ti.App.Properties API. You learned how to store both simple and complex data in app properties and retrieve that data later.