如何以编程方式获取使用钛的Android / iPhone联系人列表

时间:2013-04-05 11:55:03

标签: android iphone titanium titanium-mobile

如何使用titanium以编程方式获取Android / iPhone的联系人列表,我需要将其作为只读用途。

我已经检查了这个Can I get a user's phone number using any of the Titanium API's?,但我想使用我的应用程序打开它,然后拨打所选号码的电话,我的应用程序将不会对号码执行任何其他操作(编辑,删除)。这在Android和iPhone中是否可以使用钛或简单的Android和iPhone?

3 个答案:

答案 0 :(得分:5)

首先,您需要按照here所述的方式访问联系人。

之后,可以通过getAllPeople

获取所有联系人

适用于iOS和Android。

在Android上执行通话很简单(您应该使用Ti.Android.ACTION_DIAL创建一个电话Intent)。在iOS上,您只需显示数字,系统应将其链接到呼叫操作。如果没有,您可以为元素添加一个监听器:

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

答案 1 :(得分:1)

您可以使用showContacts方法获取手机通讯录,如下所示

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

对于iphone,您应该需要联系人的访问权限。您可以按如下方式检查权限

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
}

有关详细信息,请参阅Titanium Contacts module

Ti.Contacts.getAllPeople也会这样做,也请参考this链接

答案 2 :(得分:0)

$.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();