首页 > 代码库 > 读取iPhone中的通讯录信息

读取iPhone中的通讯录信息

添加AddressBook这个包;
然后
#import <AddressBook/AddressBook.h>

//
取得本地通信录名柄 ABAddressBookRef addressBook; 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 results = ABAddressBookCopyArrayOfAllPeople(addressBook); for(int i = 0; i < CFArrayGetCount(results); i++){ ABRecordRef person = CFArrayGetValueAtIndex(results, i); //读取firstname NSString *firstName = (__bridge NSString*)ABRecordCopyValue(person, kABPersonFirstNameProperty); //读取lastname NSString *lastName = (__bridge NSString*)ABRecordCopyValue(person, kABPersonLastNameProperty); //读取电话号码 NSString *phoneNum; ABMultiValueRef phone = ABRecordCopyValue(person, kABPersonPhoneProperty); for (int k = 0; k < ABMultiValueGetCount(phone); k++){ phoneNum = (__bridge NSString*)ABMultiValueCopyValueAtIndex(phone, k); } NSLog(@"firstName:%@ - lastName:%@ - phoneNum:%@",firstName,lastName,phoneNum); }

 

读取iPhone中的通讯录信息