iPhone地址簿的电话号码



我正在做一个iPhone应用程序在Xcode需要电话簿访问。我需要地址簿中的所有电话号码,并且应该存储在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,如果如果你有以下版本则不需要写if条件。将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];
        }
   }

最新更新