当前位置:首页 > 通信资讯 > 正文

ios开发获取本机号码(ios app获取通讯录)

一、在工程中添加addressbook.framework和addressbookui.framework

二、获取通讯录

1、在infterface中定义数组并在init方法中初始化

复制代码 代码如下:
nsmutablearray *addressbooktemp;

- (id)initwithnibname:(nsstring *)nibnameornil bundle:(nsbundle *)nibbundleornil
{
addressbooktemp = [nsmutablearray array];
}


2、定义一个model,用来存放通讯录中的各个属性
新建一个继承自nsobject的类,在.h中

复制代码 代码如下:
@interface tkaddressbook : nsobject {
nsinteger sectionnumber;
nsinteger recordid;
nsstring *name;
nsstring *email;
nsstring *tel;
}
@property nsinteger sectionnumber;
@property nsinteger recordid;
@property (nonatomic, retain) nsstring *name;
@property (nonatomic, retain) nsstring *email;
@property (nonatomic, retain) nsstring *tel;

@end


在.m文件中进行synthesize

复制代码 代码如下:
@implementation tkaddressbook
@synthesize name, email, tel, recordid, sectionnumber;

@end


3、获取联系人

在ios6之后,获取通讯录需要获得权限

复制代码 代码如下:


//新建一个通讯录类
abaddressbookref addressbooks = nil;

if ([[uidevice currentdevice].systemversion floatvalue] >= 6.0)

{
addressbooks = abaddressbookcreatewithoptions(null, null);

//获取通讯录权限

dispatch_semaphore_t sema = dispatch_semaphore_create(0);

abaddressbookrequestaccesswithcompletion(addressbooks, ^(bool granted, cferrorref error){dispatch_semaphore_signal(sema);});

dispatch_semaphore_wait(sema, dispatch_time_forever);

dispatch_release(sema);

}

else

{
addressbooks = abaddressbookcreate();

}

//获取通讯录中的所有人
cfarrayref allpeople = abaddressbookcopyarrayofallpeople(addressbooks);

//通讯录中人数
cfindex npeople = abaddressbookgetpersoncount(addressbooks);

//循环,获取每个人的个人信息
for (nsinteger i = 0; i < npeople; i++)
{
//新建一个addressbook model类
tkaddressbook *addressbook = [[tkaddressbook alloc] init];
//获取个人
abrecordref person = cfarraygetvalueatindex(allpeople, i);
//获取个人名字
cftyperef abname = abrecordcopyvalue(person, kabpersonfirstnameproperty);
cftyperef ablastname = abrecordcopyvalue(person, kabpersonlastnameproperty);
cfstringref abfullname = abrecordcopycompositename(person);
nsstring *namestring = (__bridge nsstring *)abname;
nsstring *lastnamestring = (__bridge nsstring *)ablastname;

if ((__bridge id)abfullname != nil) {
namestring = (__bridge nsstring *)abfullname;
} else {
if ((__bridge id)ablastname != nil)
{
namestring = [nsstring stringwithformat:@"%@ %@", namestring, lastnamestring];
}
}
addressbook.name = namestring;
addressbook.recordid = (int)abrecordgetrecordid(person);;

abpropertyid multiproperties[] = {
kabpersonphoneproperty,
kabpersonemailproperty
};
nsinteger multipropertiestotal = sizeof(multiproperties) / sizeof(abpropertyid);
for (nsinteger j = 0; j < multipropertiestotal; j++) {
abpropertyid property = multiproperties[j];
abmultivalueref valuesref = abrecordcopyvalue(person, property);
nsinteger valuescount = 0;
if (valuesref != nil) valuescount = abmultivaluegetcount(valuesref);

if (valuescount == 0) {
cfrelease(valuesref);
continue;
}
//获取电话号码和email
for (nsinteger k = 0; k < valuescount; k++) {
cftyperef value = abmultivaluecopyvalueatindex(valuesref, k);
switch (j) {
case 0: {// phone number
addressbook.tel = (__bridge nsstring*)value;
break;
}
case 1: {// email
addressbook.email = (__bridge nsstring*)value;
break;
}
}
cfrelease(value);
}
cfrelease(valuesref);
}
//将个人信息添加到数组中,循环完成后addressbooktemp中包含所有联系人的信息
[addressbooktemp addobject:addressbook];

if (abname) cfrelease(abname);
if (ablastname) cfrelease(ablastname);
if (abfullname) cfrelease(abfullname);
}


三、显示在table中

复制代码 代码如下:


//行数
- (nsinteger)numberofsectionsintableview:(uitableview *)tableview {
return 1;
}

//列数
- (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section {
return [addressbooktemp count];
}

//cell内容
- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath {

nsstring *cellidentifier = @"contactcell";

uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:cellidentifier];

if (cell == nil){
cell = [[uitableviewcell alloc] initwithstyle:uitableviewcellstylesubtitle reuseidentifier:cellidentifier];
}

tkaddressbook *book = [addressbooktemp objectatindex:indexpath.row];

cell.textlabel.text = book.name;

cell.detailtextlabel.text = book.tel;

return cell;
}


列表效果

ios开发获取本机号码(ios app获取通讯录)

ps:通讯录中电话号码中的"-"可以在存入数组之前进行处理,属于nsstring处理的范畴,解决办法有很多种,本文不多加说明

四、删除通讯录数据

复制代码 代码如下:
<span style="white-space:pre"> </span>abrecordref person = objc_unretainedpointer([mycontacts objectatindex:indexpath.row]);
cferrorref *error;
abaddressbookremoverecord(addressbook, person, error);
abaddressbooksave(addressbook, error);
mycontacts = nil;
[tableview deleterowsatindexpaths:[nsarray arraywithobject:indexpath] withrowanimation:uitableviewrowanimationfade];
如果您对该产品感兴趣,请填写办理(客服微信:xiaoxiongyidong)

为您推荐:

发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。