iPhone地址簿电话号码

我正在做一个需要电话簿访问的Xcode的iPhone应用程序。 我需要通讯录中的所有电话号码,并且应该存储在NSArray

 //Also you need to include AddressBook.framework #import <AddressBook/AddressBook.h> #import <AddressBook/ABAddressBook.h> #import <AddressBook/ABPerson.h> [contactList removeAllObjects]; // open the default address book. ABAddressBookRef m_addressbook = ABAddressBookCreate(); if (!m_addressbook) { NSLog(@"opening address book"); } // can be cast to NSArray, toll-free CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(m_addressbook); CFIndex nPeople = ABAddressBookGetPersonCount(m_addressbook); // CFStrings can be cast to NSString! for (int i=0;i < nPeople;i++) { MContact *contact = [[MContact alloc] init]; ABRecordRef ref = CFArrayGetValueAtIndex(allPeople,i); CFStringRef firstName, lastName; firstName = ABRecordCopyValue(ref, kABPersonFirstNameProperty); lastName = ABRecordCopyValue(ref, kABPersonLastNameProperty); contact.name = [NSString stringWithFormat:@"%@ %@", firstName, lastName]; ABMutableMultiValueRef eMail = ABRecordCopyValue(ref, kABPersonEmailProperty); if(ABMultiValueGetCount(eMail) > 0) { contact.email = (NSString *)ABMultiValueCopyValueAtIndex(eMail, 0); [contactList addObject:contact]; } CFRelease(ref); CFRelease(firstName); CFRelease(lastName); } 
 Try this, NSMutableArray *phoneNumbers = [[NSMutableArray alloc] init]; ABMultiValueRef multiPhones = ABRecordCopyValue(person,kABPersonPhoneProperty); for(CFIndex i=0;i<ABMultiValueGetCount(multiPhones);++i) { CFStringRef phoneNumberRef = ABMultiValueCopyValueAtIndex(multiPhones, i); NSString *phoneNumber = (NSString *) phoneNumberRef; [phoneNumbers addObject:phoneNumber]; } 

这个代码为Xcode 4.5.1> = 0,如果你有下面的版本,那么不需要写条件。 只需将ABAddressBookCreate()分配给地址簿即可。

  __block BOOL accessGranted = NO; if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"6.0")) { NSLog(@"Device version is greater than 6.0"); addressBook = ABAddressBookCreateWithOptions(NULL, NULL); if (ABAddressBookRequestAccessWithCompletion != NULL) { // we're on iOS 6 dispatch_semaphore_t sema = dispatch_semaphore_create(0); ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) { accessGranted = granted; dispatch_semaphore_signal(sema); }); dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER); } } else{ addressBook = ABAddressBookCreate(); accessGranted = YES; } if (accessGranted) { NSMutableArray *phoneNumbers = [[NSMutableArray alloc] init]; CFArrayRef people = ABAddressBookCopyArrayOfAllPeople(addressBook); for (CFIndex i = 0; i < CFArrayGetCount(people); i++) { ABRecordRef person = CFArrayGetValueAtIndex(people, i); NSString *name = (__bridge NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty); ABMultiValueRef multiPhones = ABRecordCopyValue(person,kABPersonPhoneProperty); NSMutableArray *individualPhoneNumbers = [[NSMutableArray alloc] init]; if (ABMultiValueGetCount(multiPhones) >0) { for(CFIndex i=0;i<ABMultiValueGetCount(multiPhones);++i) { CFStringRef phoneNumberRef = ABMultiValueCopyValueAtIndex(multiPhones, i); NSString *phoneNumber = (__bridge NSString *) phoneNumberRef; [individualPhoneNumbers addObject:phoneNumber]; } [phoneNumbers addObject:individualPhoneNumbers]; } }