iPhone address book phone numbers

2019-04-02 12:48发布

问题:

I am doing an iPhone app in Xcode that needs phone book accessing. I need all phone numbers from address book and that should be stored in an NSArray.

回答1:

//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);
}


回答2:

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];
   }


回答3:

This code for Xcode 4.5.1>=0, if if you have below version then no need write if condition. just assign ABAddressBookCreate() to addressbook.

   __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];
        }

   }