第一步要引入框架和头文件
#import <AddressBookUI/AddressBookUI.h>
#import <AddressBook/AddressBook.h>
第二步判断通讯录的访问状态状态
/*用户已经设置过禁止访问*/
if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusDenied) {
return ;
}
/*查看是否可以处理通讯录*/
/*如果第一次会弹出授权界面*/
CFErrorRef error = NULL;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);
/*访问权限的设定*/
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
if (granted) {//访问成功之后调用
/*处理通讯录的一些东西*/;
}
});
CFRelease(addressBook);
在第二步成功的前提下,获取通讯录信息的数组,其中数组中的每一个元素就是一个联系人
CFErrorRef error = NULL;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);
NSArray *personArray;
if([searchText length]==0){
//查询所有
personArray = CFBridgingRelease(ABAddressBookCopyArrayOfAllPeople(addressBook));
} else {
//条件查询
CFStringRef cfSearchText = (CFStringRef)CFBridgingRetain(searchText);//按照关键字查询
personArray = CFBridgingRelease(ABAddressBookCopyPeopleWithName(addressBook, cfSearchText));
CFRelease(cfSearchText);
}
取到单一的联系人,然后获取联系人的属性
for (int i=0; i<personArray.count; i++) {
ABRecordRef thisPerson = CFBridgingRetain([personArray objectAtIndex:i]);//单个联系人对象
/*单值属性*/
//获取用户名
NSString *firstName = CFBridgingRelease(ABRecordCopyValue(thisPerson, kABPersonFirstNameProperty));//联系人对象的单值属性
firstName = firstName != nil?firstName:@"";
NSString *lastName = CFBridgingRelease(ABRecordCopyValue(thisPerson, kABPersonLastNameProperty));
lastName = lastName != nil?lastName:@"";
NSString *fullName=[NSString stringWithFormat:@"%@%@",firstName,lastName];
//获取用户头像
if (ABPersonHasImageData(thisPerson)) {
NSData *photoData = CFBridgingRelease(ABPersonCopyImageData(thisPerson));//联系人的头像信息
if(photoData){
model.personPic=[UIImage imageWithData:photoData];
}
}
/*多值属性*/
//获取用户的电话信息
ABMultiValueRef phoneNumberProperty = ABRecordCopyValue(thisPerson, kABPersonPhoneProperty);//联系人对象的多值属性
/*类型和oc中的字典相似*/
NSArray* phoneNumberArray = CFBridgingRelease(ABMultiValueCopyArrayOfAllValues(phoneNumberProperty));//多值属性所有的值
for(int index = 0; index< [phoneNumberArray count]; index++){
NSString *phoneNumber = [phoneNumberArray objectAtIndex:index];
NSString *phoneNumberLabel = CFBridgingRelease(ABMultiValueCopyLabelAtIndex(phoneNumberProperty, index));//获取和该值对应的label(名称)
//检测两项
if ([phoneNumberLabel isEqualToString:(NSString*)kABPersonPhoneMobileLabel]) {
//根据label将值传给固定的字段
model.mobileNum=[NSString stringWithFormat:@"%@",phoneNumber];
} else if ([phoneNumberLabel isEqualToString:(NSString*)kABPersonPhoneIPhoneLabel]) {
model.iphoneNum=[NSString stringWithFormat:@"%@",phoneNumber];
}
}
CFRelease(phoneNumberProperty);
}
关于单值,多值属性的那些事可以看看最原始的帖子:http://blog.csdn.net/tonny_guan/article/details/9211797
这里在结合tableview将所有的数据显示出来(按照字母排序)
这里面用到一个别人写好的文件:http://download.csdn.net/detail/u010673842/8550041
我们要做的只是调用它里面的两个方法,为我们生成:
//----- 返回tableview右方indexArray
+(NSMutableArray*)IndexArray:(NSArray*)stringArr;
//----- 返回联系人
+(NSMutableArray*)LetterSortArray:(NSArray*)stringArr;
self.indexArray=[ChineseString IndexArray:self.personName];//personname是我们在之前获取到的所有联系人的名称数组
self.LetterResultArr=[ChineseString LetterSortArray:self.personName];
再根据tableview的一些数据源方法,将这两个数组里面的值表达
#pragma mark -Section的Header的值
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
NSString *key = [self.indexArray objectAtIndex:section];
return key;
}
#pragma mark -设置右方表格的索引数组
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return self.indexArray;
}
#pragma mark -索引和section绑定
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
return index;
}
#pragma mark -允许数据源告知必须加载到Table View中的表的Section数。
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [self.indexArray count];
}
#pragma mark -设置表格的行数为数组的元素个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[self.LetterResultArr objectAtIndex:section] count];
}
#pragma mark -每一行的内容为数组相应索引的值
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
cell.textLabel.text = [[self.LetterResultArr objectAtIndex:indexPath.section]objectAtIndex:indexPath.row];
return cell;
}
然后我们可以做一个数组,让联系人名称和他的其他属性(作为以个模型)最为一个模型,这样就可以通过以个名称就可以联系到所有的属性值了