摘要: 获取本机通讯录中的内容,显示在列表(table)中, iOS6之后,苹果对系统中通讯录日历等控件的调用进行了权限控制,获取通讯录需加上请求权限部分的代码
一、在工程中添加AddressBook.framework和AddressBookUI.framework
二、获取通讯录
1.在.h文件中引入
新建一个继承自NSObject的类,在.h中
@interface AddressBook : NSObject
@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 AddressBook
@synthesize sectionNumber;
@synthesize recordID;
@synthesize name;
@synthesize email;
@synthesize tel;
@end
3、获取联系人
在iOS6之后,获取通讯录需要获得权限
//初始化一个数组,用于存放通讯录所有信息
addressBookTemp = [[NSMutableArray alloc] init];
//新建一个通讯录类
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类
AddressBook *addressBook = [[AddressBook alloc] init];
//获取个人
ABRecordRef person = CFArrayGetValueAtIndex(allPeople, i);
//获取个人名字
CFTypeRef abName = ABRecordCopyValue(person, kABPersonFirstNameProperty);
CFTypeRef abLastName = ABRecordCopyValue(person, kABPersonLastNameProperty);
CFStringRef abFullName = ABRecordCopyCompositeName(person);
NSString *nameString = (NSString *)abName;
NSString *lastNameString = (NSString *)abLastName;
if ((id)abFullName != nil)
{
nameString = (NSString *)abFullName;
}
else if ((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 (0 == valuesCount)
{
CFRelease(valuesRef);
continue;
}
//获取电话号码和email
for (NSInteger k = 0; k < valuesCount; k ++)
{
CFTypeRef value = ABMultiValueCopyValueAtIndex(valuesRef, k);
switch (j) {
case 0:
{ //phone number
addressBook.tel = (NSString *)value;
break;
}
case 1:
addressBook.email = (NSString *)value;
}
default:
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];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifer = @"ContactCell";
UITableViewCell *cell= [tableView dequeueReusableCellWithIdentifier:cellIdentifer];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifer];
}
AddressBook *book = [addressBookTemp objectAtIndex:indexPath.row];
cell.textLabel.text = book.name;
cell.detailTextLabel.text = book.tel;
NSLog(@"%i = %i,%@", indexPath.row,book.recordID,book.email);
return cell;
}
列表效果: