Ensuring your device can make phone calls

With all of the technical wizardry and touch-screen goodness that are now packed into modern day smartphones, it's easy to forget that their primary function is still that of a telephone which is capable of making and receiving voice calls. However, there may be times when the user's device is not capable of performing a call for whatever reason (poor network service, lack of call functionality that is iPod touch user, and so on).

In this recipe, we will attempt to make a phone call by first checking on the device's capabilities and throwing an error message when this is not possible.

Note

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

How to do it…

Open your app.js file, remove any existing code, and type in the following:

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

//create the textfield number entry to dial
var txtNumber = Titanium.UI.createTextField({
    top: 20,
    left: 20,
    height: 40,
    width: 280,
    hintText: '+44 1234 321 231',
    borderStyle: 1
});
win1.add(txtNumber);

//create our call button
var btnCall = Titanium.UI.createButton({
    top: 90,
    left: 20,
    width: 280,
    height: 40,
    title: 'Call Now!'
});

//attempt a phone call
btnCall.addEventListener('click', function(e){
 if(txtNumber.value != '') 
 {
    if(Titanium.Platform.osname != 'ipad' 
    && Titanium.Platform.model != 'iPod Touch'
    && Titanium.Platform.model != 'google_sdk' 
    && Titanium.Platform.model != 'Simulator')
    {
        Titanium.Platform.openURL('tel:' + txtNumber.value);
    }
    else
    {
        alert("Sorry, your device is not capable of making calls.");
    }
 }
 else 
 {
    alert("You must provide a valid phone number!");
 }  
});
win1.add(btnCall);


//open root window
win1.open();

Run your application now either in the simulator or on a device not capable of making calls, such as an iPod Touch. You should see an alert appear stating that the device can not action the requested phone call.

How it works…

Here we are simply using the Titanium Platform namespace to determine what kind of device the user is currently using, and providing an error message when that device is of the type iPod, iPad, or the emulator, as seen in the following screenshot. If the device in question is capable of making phone calls, such as the iPhone or an Android smartphone, then the device's phone API is called via means of a special URL request:

//must be a valid number, e.g. 'tel:07427555122'
Titanium.Platform.openURL('tel:' + txtNumber.value);

As long as the phone number being passed is valid, the device will launch the calling screen and attempt to place the call on the user's behalf.

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.144.9.169