一、在工程中添加AddressBook.framework和AddressBookUI.framework
二、获取通讯录
1、在infterface中定义数组并在init方法中初始化
NSMutableArray *addressBookTemp; addressBookTemp=[NSMutableArray array];
2、定义一个model,用来存放通讯录中的各个属性
新建一个继承自NSObject的类,在.h中
@interface TKAddressBook : NSObject{ NSInteger sectionNumber; NSInteger *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
3、获取联系人
在iOS6之后,获取通讯录需要获得权限
//获取联系人 -(void)ReadAllProple{ //取得本地通信录民柄 ABAddressBookRef addressBook=nil; if ([[UIDevice currentDevice].systemVersion floatValue]>=6.0) { addressBook =ABAddressBookCreateWithOptions(NULL, NULL); //获取通讯录权限 dispatch_semaphore_t sema=dispatch_semaphore_create(0); ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) { dispatch_semaphore_signal(sema); }); dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER); }else{ addressBook=ABAddressBookCreate(); } //获取通讯录中的所有人 CFArrayRef allPeople=ABAddressBookCopyArrayOfAllPeople(addressBook); NSLog(@"all---%@",allPeople); //通讯录人数 CFIndex nPeople=ABAddressBookGetPersonCount(addressBook); NSLog(@"count---%li",nPeople); //获取每个人得信息 for (NSInteger i=0; i<nPeople; i++) { //新建一个addressbook model类 TKAddressBook *addBook=[[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]; } } NSLog(@"nastring----%@",nameString); addBook.name=nameString; addBook.recordID=(int)ABRecordGetRecordID(person); ABPropertyID multiPeroperties[]={ kABPersonPhoneProperty,//读取手机号码 kABPersonEmailProperty,//读取邮箱 // kABPersonFirstNameProperty,//读取firstname // kABPersonLastNameProperty,//读取lastname // kABPersonMiddleNameProperty,//读取middlename // kABPersonPrefixProperty,// 读取prefix前缀 // kABPersonSuffixProperty,//读取suffix后缀 // kABPersonNicknameProperty,//读取nickname呢称 // kABPersonFirstNamePhoneticProperty,//读取firstname拼音音标 // kABPersonLastNamePhoneticProperty,//读取lastname拼音音标 // kABPersonMiddleNamePhoneticProperty,//读取middlename拼音音标 // kABPersonOrganizationProperty,//读取organization公司 // kABPersonJobTitleProperty,//读取jobtitle工作 // kABPersonDepartmentProperty,//读取department部门 // kABPersonBirthdayProperty,//读取birthday生日 // kABPersonNoteProperty,//读取note备忘录 // kABPersonCreationDateProperty,//第一次添加该条记录的时间 // kABPersonModificationDateProperty,//最后一次修改該条记录的时间 }; NSInteger multiPropertiesTotal=sizeof(multiPeroperties)/sizeof(ABPropertyID); for (NSInteger j = 0; j < multiPropertiesTotal; j++) { ABPropertyID property = multiPeroperties[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 addBook.tel = (__bridge NSString*)value; break; } case 1: {// Email addBook.email = (__bridge NSString*)value; break; } } CFRelease(value); } CFRelease(valuesRef); } //将个人信息添加到数组中,循环完成后addressBookTemp中包含所有联系人得信息 [addressBookTemp addObject:addBook]; } NSLog(@"b ook----%@",addressBookTemp); }
然后就可以用TableView显示出来:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *[email protected]"acell"; MailListTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:acell]; if (!cell) { cell=[[[NSBundle mainBundle]loadNibNamed:@"MailListTableViewCell" owner:self options:nil]lastObject]; } cell.selectionStyle=UITableViewCellSelectionStyleNone; TKAddressBook *book=[addressBookTemp objectAtIndex:indexPath.row]; cell.lalName.text=book.name; cell.lalPhone.text=book.tel; // [email protected]"怪咖啡是腾讯自制的网络情景喜剧,在剧中的启萱是一个“怪咖啡”店长,个性孤傲、说话一针见血的女汉子。"; cell.lalRemarks.text=book.email; return cell; }
以上就是整个功能的核心代码,效果如下:
读取本机通讯录的基本功能已完成,其他的未写出来的属性可参考一下两个连接:
http://www.cocoachina.com/bbs/read.php?tid=62527
http://www.cnblogs.com/qingjoin/archive/2012/11/19/2777212.html
时间: 2024-11-13 08:14:57