iOS读取通讯录中的各种信息
//读取所有联系人
-(void)ReadAllPeoples
{
//取得本地通信录名柄
ABAddressBookRef tmpAddressBook = nil;
if ([[UIDevice currentDevice].systemVersion floatValue]>=6.0) {
tmpAddressBook=ABAddressBookCreateWithOptions(NULL, NULL);
dispatch_semaphore_t sema=dispatch_semaphore_create(0);
ABAddressBookRequestAccessWithCompletion(tmpAddressBook, ^(bool greanted, CFErrorRef error){
dispatch_semaphore_signal(sema);
});
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
}
else
{
tmpAddressBook =ABAddressBookCreate();
}
//取得本地所有联系人记录
if (tmpAddressBook==nil) {
return ;
};
NSArray* tmpPeoples = (__bridge NSArray*)ABAddressBookCopyArrayOfAllPeople(tmpAddressBook);
for(id tmpPerson in tmpPeoples)
{
//获取的联系人单一属性:First name
NSString* tmpFirstName = (__bridge NSString*)ABRecordCopyValue((__bridge ABRecordRef)(tmpPerson), kABPersonFirstNameProperty);
NSLog(@"First name:%@", tmpFirstName);
//获取的联系人单一属性:Last name
NSString* tmpLastName = (__bridge NSString*)ABRecordCopyValue((__bridge ABRecordRef)(tmpPerson), kABPersonLastNameProperty);
NSLog(@"Last name:%@", tmpLastName);
//获取的联系人单一属性:Nickname
NSString* tmpNickname = (__bridge NSString*)ABRecordCopyValue((__bridge ABRecordRef)(tmpPerson), kABPersonNicknameProperty);
NSLog(@"Nickname:%@", tmpNickname);
//获取的联系人单一属性:Company name
NSString* tmpCompanyname = (__bridge NSString*)ABRecordCopyValue((__bridge ABRecordRef)(tmpPerson), kABPersonOrganizationProperty);
NSLog(@"Company name:%@", tmpCompanyname);
//获取的联系人单一属性:Job Title
NSString* tmpJobTitle= (__bridge NSString*)ABRecordCopyValue((__bridge ABRecordRef)(tmpPerson), kABPersonJobTitleProperty);
NSLog(@"Job Title:%@", tmpJobTitle);
//获取的联系人单一属性:Department name
NSString* tmpDepartmentName = (__bridge NSString*)ABRecordCopyValue((__bridge ABRecordRef)(tmpPerson), kABPersonDepartmentProperty);
NSLog(@"Department name:%@", tmpDepartmentName);
//获取的联系人单一属性:Email(s)
ABMultiValueRef tmpEmails = ABRecordCopyValue((__bridge ABRecordRef)(tmpPerson), kABPersonEmailProperty);
for(NSInteger j = 0; j < ABMultiValueGetCount(tmpEmails); j++)
{
NSString* tmpEmailIndex = (__bridge NSString*)ABMultiValueCopyValueAtIndex(tmpEmails, j);
NSLog(@"Emails%ld:%@", (long)j, tmpEmailIndex);
}
CFRelease(tmpEmails);
//获取的联系人单一属性:Birthday
NSDate* tmpBirthday = (__bridge NSDate*)ABRecordCopyValue((__bridge ABRecordRef)(tmpPerson), kABPersonBirthdayProperty);
NSLog(@"Birthday:%@", tmpBirthday);
//获取的联系人单一属性:Note
NSString* tmpNote = (__bridge NSString*)ABRecordCopyValue((__bridge ABRecordRef)(tmpPerson), kABPersonNoteProperty);
NSLog(@"Note:%@", tmpNote);
//获取的联系人单一属性:Generic phone number
ABMultiValueRef tmpPhones = ABRecordCopyValue((__bridge ABRecordRef)(tmpPerson), kABPersonPhoneProperty);
for(NSInteger j = 0; j < ABMultiValueGetCount(tmpPhones); j++)
{
NSString* tmpPhoneIndex = (__bridge NSString*)ABMultiValueCopyValueAtIndex(tmpPhones, j);
NSLog(@"tmpPhoneIndex%ld:%@", (long)j, tmpPhoneIndex);
}
CFRelease(tmpPhones);
}
//释放内存
CFRelease(tmpAddressBook);
}
评论已关闭