Ios读取通讯录联系人数据

iOS技巧之获取本机通讯录中的内容,解析通讯录源代码

慕课网,程序员升职加薪神器,点击免费学习

摘要获取本机通讯录中的内容,显示在列表(table)中,iOS6之后,苹果对系统中通讯录日历等控件的调用进行了权限控制,获取通讯录需加上请求权限部分的代码

iOS获取通讯录通讯录iOS调用联系人iOS6通讯录ABAddressBookCreateWithOptions

一、在工程中添加AddressBook.framework和AddressBookUI.framework

二、获取通讯录

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

NSMutableArray*addressBookTemp;

-(id)initWithNibName:(NSString*)nibNameOrNilbundle:(NSBundle*)nibBundleOrNil

{

addressBookTemp=[NSMutableArrayarray];

}

2、定义一个model,用来存放通讯录中的各个属性

新建一个继承自NSObject的类,在.h中

@interfaceTKAddressBook:NSObject{

NSIntegersectionNumber;

NSIntegerrecordID;

NSString*name;

NSString*email;

NSString*tel;

}

@propertyNSIntegersectionNumber;

@propertyNSIntegerrecordID;

@property(nonatomic,retain)NSString*name;

@property(nonatomic,retain)NSString*email;

@property(nonatomic,retain)NSString*tel;

@end

在.m文件中进行synthesize

@implementationTKAddressBook

@synthesizename,email,tel,recordID,sectionNumber;

@end

3、获取联系人

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

//新建一个通讯录类

ABAddressBookRefaddressBooks=nil;

if([[UIDevicecurrentDevice].systemVersionfloatValue]>=6.0)

{

addressBooks=ABAddressBookCreateWithOptions(NULL,NULL);

//获取通讯录权限

dispatch_semaphore_tsema=dispatch_semaphore_create(0);

ABAddressBookRequestAccessWithCompletion(addressBooks,^(boolgranted,CFErrorReferror){dispatch_semaphore_signal(sema);});

dispatch_semaphore_wait(sema,DISPATCH_TIME_FOREVER);

dispatch_release(sema);

}

else

{

addressBooks=ABAddressBookCreate();

}

//获取通讯录中的所有人

CFArrayRefallPeople=ABAddressBookCopyArrayOfAllPeople(addressBooks);

//通讯录中人数

CFIndexnPeople=ABAddressBookGetPersonCount(addressBooks);

//循环,获取每个人的个人信息

for(NSIntegeri=0;i<nPeople;i++)

{

//新建一个addressBookmodel类

TKAddressBook*addressBook=[[TKAddressBookalloc]init];

//获取个人

ABRecordRefperson=CFArrayGetValueAtIndex(allPeople,i);

//获取个人名字

CFTypeRefabName=ABRecordCopyValue(person,kABPersonFirstNameProperty);

CFTypeRefabLastName=ABRecordCopyValue(person,kABPersonLastNameProperty);

CFStringRefabFullName=ABRecordCopyCompositeName(person);

NSString*nameString=(__bridgeNSString*)abName;

NSString*lastNameString=(__bridgeNSString*)abLastName;

if((__bridgeid)abFullName!=nil){

nameString=(__bridgeNSString*)abFullName;

}else{

if((__bridgeid)abLastName!=nil)

{

nameString=[NSStringstringWithFormat:@"%@%@",nameString,lastNameString];

}

}

addressBook.name=nameString;

addressBook.recordID=(int)ABRecordGetRecordID(person);;

ABPropertyIDmultiProperties[]={

kABPersonPhoneProperty,

kABPersonEmailProperty

};

NSIntegermultiPropertiesTotal=sizeof(multiProperties)/sizeof(ABPropertyID);

for(NSIntegerj=0;j<multiPropertiesTotal;j++){

ABPropertyIDproperty=multiProperties[j];

ABMultiValueRefvaluesRef=ABRecordCopyValue(person,property);

NSIntegervaluesCount=0;

if(valuesRef!=nil)valuesCount=ABMultiValueGetCount(valuesRef);

if(valuesCount==0){

CFRelease(valuesRef);

continue;

}

//获取电话号码和email

for(NSIntegerk=0;k<valuesCount;k++){

CFTypeRefvalue=ABMultiValueCopyValueAtIndex(valuesRef,k);

switch(j){

case0:{//Phonenumber

addressBook.tel=(__bridgeNSString*)value;

break;

}

case1:{//Email

addressBook.email=(__bridgeNSString*)value;

break;

}

}

CFRelease(value);

}

CFRelease(valuesRef);

}

//将个人信息添加到数组中,循环完成后addressBookTemp中包含所有联系人的信息

[addressBookTempaddObject:addressBook];

if(abName)CFRelease(abName);

if(abLastName)CFRelease(abLastName);

if(abFullName)CFRelease(abFullName);

}

三、显示在table中

//行数

-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView{

return1;

}

//列数

-(NSInteger)tableView:(UITableView*)tableViewnumberOfRowsInSection:(NSInteger)section{

return[addressBookTempcount];

}

//cell内容

-(UITableViewCell*)tableView:(UITableView*)tableViewcellForRowAtIndexPath:(NSIndexPath*)indexPath{

NSString*cellIdentifier=@"ContactCell";

UITableViewCell*cell=[tableViewdequeueReusableCellWithIdentifier:cellIdentifier];

if(cell==nil){

cell=[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:cellIdentifier];

}

TKAddressBook*book=[addressBookTempobjectAtIndex:indexPath.row];

cell.textLabel.text=book.name;

cell.detailTextLabel.text=book.tel;

returncell;

}

相关推荐