Gathering information about your device

The majority of information about the current device is available through the Titanium.Platform namespace. It is here that we can determine a host of device-specific data, including the battery level, device OS and version, current device language, the screen resolution, and more. Knowing this information is important, as it will give you a series of clues as to what is happening on the physical device. One example is that you may wish to back up a user's application data if the battery dips below a certain percentage, in case the device was to shut down and data was lost. More commonly, you will use device properties such as Titanium.Platform.osname to determine what operating system your app is currently running under, such as iPhone, iPad, or Android.

Getting ready

To prepare for this recipe, open up Titanium Studio and log in if you have not already done so. If you need to register a new account, you can do so for free directly from within the application. Once you are logged in, click on New Project, and the details window for creating a new project will appear. Enter in PlatformDiffs as the name of the app, and fill in the rest of the details with your own information. Open the app.js file and remove everything apart from the instantiation of the root window and the win1 object's open method, so that it looks like the following:

//
// create root window
//
var win1 = Titanium.UI.createWindow({  
    title:'Tab 1',
    backgroundColor:'#fff'
});

//open root window
win1.open();

Note

The complete source code for this recipe can be found in the /Chapter 11/Recipe 1 folder.

How to do it…

Now, back in the app.js file, we are going to simply create a number of labels and request the values for each from the properties available to us in the Titanium.Platform namespace. These values will then be displayed as text on-screen:

var labelOS = Titanium.UI.createLabel({
    width:  'auto',
    height: 30,
    top: 0,
    left: 10,
    font: {fontSize: 14, fontFamily: 'Helvetica'},
    color: '#000',
    text: 'OS Details: ' + Titanium.Platform.osname + ' (version ' + Titanium.Platform.version + ')'
});

var labelBattery = Titanium.UI.createLabel({
    width:  'auto',
    height: 30,
    top: 40,
    left: 10,
    font: {fontSize: 14, fontFamily: 'Helvetica'},
    color: '#000',
    text: 'Battery level: ' + Titanium.Platform.batteryLevel
});

var labelMemory = Titanium.UI.createLabel({
    width:  'auto',
    height: 30,
    top: 80,
    left: 10,
    font: {fontSize: 14, fontFamily: 'Helvetica'},
    color: '#000',
    text: 'Available memory: ' + Titanium.Platform.availableMemory + 'MB'
});

var labelArchitecture = Titanium.UI.createLabel({
    width:  'auto',
    height: 30,
    top: 120,
    left: 10,
    font: {fontSize: 14, fontFamily: 'Helvetica'},
    color: '#000',
    text: 'Architecture: ' + Titanium.Platform.architecture
});

var labelLocale = Titanium.UI.createLabel({
    width:  'auto',
    height: 30,
    top: 160,
    left: 10,
    font: {fontSize: 14, fontFamily: 'Helvetica'},
    color: '#000',
    text: 'Locale: ' + Titanium.Platform.locale
});

var labelModel = Titanium.UI.createLabel({
    width:  'auto',
    height: 30,
    top: 200,
    left: 10,
    font: {fontSize: 14, fontFamily: 'Helvetica'},
    color: '#000',
    text: 'Model: ' + Titanium.Platform.model
});


win1.add(labelOS);
win1.add(labelBattery);
win1.add(labelMemory);
win1.add(labelArchitecture);
win1.add(labelLocale);
win1.add(labelModel);

How it works…

Each of the labels in this code sample represents a different piece of information about your device and its capabilities. There is nothing particularly complicated about the code here but it's the methods themselves that are important.

Most of these are pretty self-explanatory. The methods for Battery, Memory, Architecture, and Model all provide you with information about the device and its specific capabilities. You may use these at certain times during your application's lifecycle, for instance, auto-saving data on a form when the battery reaches a certain critical level.

The most useful of all of these methods is Titanium.Platform.osname. It is this method that you will use constantly throughout the development of a Titanium cross-platform app as you will use it to check whether you're on iPhone or the Android platform, as seen in the following screenshot, and run certain code depending on that result.

How it works…
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.72.74