NoNaMe
NoNaMe

Reputation: 6232

How can I programmatically get the contact list of Android/iPhone using titanium

How can I programmatically get the contact list of Android/iPhone using titanium, I need this for read only purpose.

I already checked this Can I get a user's phone number using any of the Titanium API's?, but I like to open it using my app and then dial a call to the selected number, my app will not perform any other action(edit, delete) on the number. Is that possible in Android and iPhone using titanium or simple Android and iPhone?

Upvotes: 2

Views: 1796

Answers (3)

Ankit Garg
Ankit Garg

Reputation: 11

$.index.open();
var people = Titanium.Contacts.getAllPeople();
var totalContacts = people.length;
Ti.UI.setBackgroundColor('#F0FFFF');
var data = [];
    var win = Ti.UI.createWindow({
    backgroundColor : 'white',
});

var view = Ti.UI.createView({
    height : "50dp",
    width : "100%",
    top : '0dp',
    backgroundColor : '#050505',
});

var text = Ti.UI.createLabel({
    text : "Contact Book",
    left : 20,
    color : '#fff'

});

view.add(text);
win.add(view);

var template = {
    childTemplates : [{
        type : 'Ti.UI.Button',
        bindId : 'image',
        properties : {
            left : '2dp',
            backgroundImage : 'appicon.png',
        }
    }, {
        type : 'Ti.UI.Label',
        bindId : 'rowtitle',
        properties : {
            left : '70dp'
        }
    }]
};
if( totalContacts > 0 )
{ 
    for( var index = 0; index < totalContacts; index++ )
    {
         var person = people[index];
        Titanium.API.info(person.fullName);
        //table.add(person.fullName);
        if(person.fullName != null){
            data.push({
                rowtitle : {
                    text :person.fullName
                },

            });
        }
    }
}

var listView = Ti.UI.createListView({
    top : '55dp',
    templates : {
        'plain' : template
    },

    defaultItemTemplate : 'plain',
});

var section = Ti.UI.createListSection({
    items : data
});

listView.sections = [section];

win.add(listView);
win.open();

Upvotes: 0

Anand
Anand

Reputation: 5332

You can get your phone contacts using showContacts method as follows

Titanium.Contacts.showContacts(); //Will display the native contacts in both iphone and android

In the case of iphone, you should need the access permission for contacts. You can check for the permission as follows

if (Ti.Contacts.contactsAuthorization == Ti.Contacts.AUTHORIZATION_AUTHORIZED){
    //You've authorization
    //Some code here
} else if (Ti.Contacts.contactsAuthorization == Ti.Contacts.AUTHORIZATION_UNKNOWN){
    Ti.Contacts.requestAuthorization(function(e){
    //Authorization is unknown so requesting for authorization
    if (e.success) {
            //You've authorization
            //Some code here
        } else {
            //No authorization hence you cannot access contatcs
        }
    });
} else {
    //No authorization hence you cannot access contatcs
}

For more details refer Titanium Contacts module.

Ti.Contacts.getAllPeople will also do the same, refer this link also

Upvotes: 1

mr.VVoo
mr.VVoo

Reputation: 2270

First you need to gain access to contacts as described here.

After that it is possible to get all contacts via getAllPeople

This works for both iOS and Android.

Performing a call works simple on Android (you should create a Phone Call Intent with Ti.Android.ACTION_DIAL). On iOS you simply need display the number and the system should link it to a call action. If not you can add a listener to the element:

label.addEventListener('click', function(e) {
  Ti.Platform.openURL('tel:<number');
});

Upvotes: 5

Related Questions