-(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 = CFBridgingRelease(ABAddressBookCopyArrayOfAllPeople(tmpAddressBook)); for(id tmpPerson in tmpPeoples) { //获取的联系人单一属性:First name NSString* tmpFirstName = CFBridgingRelease(ABRecordCopyValue(CFBridgingRetain(tmpPerson), kABPersonFirstNameProperty)); NSLog(@"First name:%@", tmpFirstName); //获取的联系人单一属性:Last name NSString* tmpLastName = CFBridgingRelease(ABRecordCopyValue(CFBridgingRetain(tmpPerson), kABPersonLastNameProperty)); NSLog(@"Last name:%@", tmpLastName); //获取的联系人单一属性:Nickname NSString* tmpNickname = CFBridgingRelease(ABRecordCopyValue(CFBridgingRetain(tmpPerson), kABPersonNicknameProperty)); NSLog(@"Nickname:%@", tmpNickname); //获取的联系人单一属性:Company name NSString* tmpCompanyname = CFBridgingRelease(ABRecordCopyValue(CFBridgingRetain(tmpPerson), kABPersonOrganizationProperty)); NSLog(@"Company name:%@", tmpCompanyname); //获取的联系人单一属性:Job Title NSString* tmpJobTitle= CFBridgingRelease(ABRecordCopyValue(CFBridgingRetain(tmpPerson), kABPersonJobTitleProperty)); NSLog(@"Job Title:%@", tmpJobTitle); //获取的联系人单一属性:Department name NSString* tmpDepartmentName = CFBridgingRelease(ABRecordCopyValue(CFBridgingRetain(tmpPerson), kABPersonDepartmentProperty)); NSLog(@"Department name:%@", tmpDepartmentName); //获取的联系人单一属性:Email(s) ABMultiValueRef tmpEmails = ABRecordCopyValue(CFBridgingRetain(tmpPerson), kABPersonEmailProperty); for(NSInteger j = 0; j < ABMultiValueGetCount(tmpEmails); j++) { NSString* tmpEmailIndex = CFBridgingRelease(ABMultiValueCopyValueAtIndex(tmpEmails, j)); NSLog(@"Emails%ld:%@", j, tmpEmailIndex); } CFRelease(tmpEmails); //获取的联系人单一属性:Birthday NSDate* tmpBirthday = CFBridgingRelease(ABRecordCopyValue(CFBridgingRetain(tmpPerson), kABPersonBirthdayProperty)); NSLog(@"Birthday:%@", tmpBirthday); //获取的联系人单一属性:Note NSString* tmpNote = CFBridgingRelease(ABRecordCopyValue(CFBridgingRetain(tmpPerson), kABPersonNoteProperty)); NSLog(@"Note:%@", tmpNote); //获取的联系人单一属性:Generic phone number ABMultiValueRef tmpPhones = ABRecordCopyValue(CFBridgingRetain(tmpPerson), kABPersonPhoneProperty); for(NSInteger j = 0; j < ABMultiValueGetCount(tmpPhones); j++) { NSString *phone = nil; NSString* tmpPhoneIndex = CFBridgingRelease(ABMultiValueCopyValueAtIndex(tmpPhones, j)); NSLog(@"tmpPhoneIndex%ld:%@", j, tmpPhoneIndex); NSString *phoneNumberLabel = CFBridgingRelease(ABMultiValueCopyLabelAtIndex(tmpPhones, j)); if ([phoneNumberLabel isEqualToString:(NSString*)kABPersonPhoneMobileLabel]) { phone = tmpPhoneIndex; NSLog(@"mobile---%@",phone); } else if ([phoneNumberLabel isEqualToString:(NSString*)kABPersonPhoneIPhoneLabel]) { phone = tmpPhoneIndex; NSLog(@"iphone---%@",phone); } } CFRelease(tmpPhones); } //释放内存 CFRelease(tmpAddressBook); }
/----------------------------------------------------------------------------------------------------------------------------
kABPersonFirstNameProperty,名字
kABPersonLastNameProperty,姓氏
kABPersonMiddleNameProperty,中间名
kABPersonPrefixProperty,前缀
kABPersonSuffixProperty,后缀
kABPersonNicknameProperty,昵称
kABPersonFirstNamePhoneticProperty,名字汉语拼音或音标
kABPersonLastNamePhoneticProperty,姓氏汉语拼音或音标
q kABPersonMiddleNamePhoneticProperty,中间名汉语拼音或音标
kABPersonOrganizationProperty,组织名
kABPersonJobTitleProperty,头衔
kABPersonDepartmentProperty,部门
kABPersonNoteProperty,备注
/----------------------------------------------------------------------------------------------------------------------------
读取多值属性
多值属性是包含多个值的集合类型,如:电话号码、Email、URL等,它们主要是由下面常量定义的:
kABPersonPhoneProperty,电话号码属性,kABMultiStringPropertyType类型多值属性;
kABPersonEmailProperty,Email属性,kABMultiStringPropertyType类型多值属性;
kABPersonURLProperty,URL属性,kABMultiStringPropertyType类型多值属性;
kABPersonRelatedNamesProperty,亲属关系人属性,kABMultiStringPropertyType类型多值属性;
kABPersonAddressProperty,地址属性,kABMultiDictionaryPropertyType类型多值属性;
kABPersonInstantMessageProperty,即时聊天属性,kABMultiDictionaryPropertyType类型多值属性;
kABPersonSocialProfileProperty,社交账号属性,kABMultiDictionaryPropertyType类型多值属性;
在多值属性中包含了label(标签)、value(值)和ID等部分,其中标签和值都是可以重复的,而ID是不能重复的
/----------------------------------------------------------------------------------------------------------------------------
kABPersonPhoneMobileLabel和kABPersonPhoneIPhoneLabel都是电话号码属性的标签。kABPersonPhoneMobileLabel是移动电话号码标签,kABPersonPhoneIPhoneLabel是iPhone电话号码标签。此外还有:
kABPersonPhoneMainLabel,主要电话号码标签;
kABPersonPhoneHomeFAXLabel,家庭传真电话号码标签;
kABPersonPhoneWorkFAXLabel,工作传真电话号码标签;
kABPersonPhonePagerLabel,寻呼机号码标签。
/---------------------------------------------------------------------------------------------------------------------------------------