Buscar contactos en iOS 7

Este código funciona bien con iOS5 / iOS6 pero no funciona con iOS7.

CFErrorRef *error = NULL; ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error); //ABAddressBookRef addressBook = ABAddressBookCreate(); CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook); CFIndex numberOfPeople = ABAddressBookGetPersonCount(addressBook); for(int i = 0; i < numberOfPeople; i++) { ABRecordRef person = CFArrayGetValueAtIndex( allPeople, i ); NSString *firstName = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonFirstNameProperty)); NSString *lastName = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonLastNameProperty)); // NSLog(@"Name:%@ %@", firstName, lastName); ABMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty); NSString *phoneNumber; for (CFIndex i = 0; i < ABMultiValueGetCount(phoneNumbers); i++) { phoneNumber = (__bridge_transfer NSString *) ABMultiValueCopyValueAtIndex(phoneNumbers, i); // NSLog(@"phone:%@", phoneNumber); } 

Hoy actualicé mi ejemplo y eliminé las pérdidas de memoria 🙂

  + (NSArray *)getAllContacts { CFErrorRef *error = nil; ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error); ABRecordRef source = ABAddressBookCopyDefaultSource(addressBook); CFArrayRef allPeople = (ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(addressBook, source, kABPersonSortByFirstName)); //CFIndex nPeople = ABAddressBookGetPersonCount(addressBook); CFIndex nPeople = CFArrayGetCount(allPeople); // bugfix who synced contacts with facebook NSMutableArray* items = [NSMutableArray arrayWithCapacity:nPeople]; if (!allPeople || !nPeople) { NSLog(@"people nil"); } for (int i = 0; i < nPeople; i++) { @autoreleasepool { //data model ContactsData *contacts = [ContactsData new]; ABRecordRef person = CFArrayGetValueAtIndex(allPeople, i); //get First Name CFStringRef firstName = (CFStringRef)ABRecordCopyValue(person,kABPersonFirstNameProperty); contacts.firstNames = [(__bridge NSString*)firstName copy]; if (firstName != NULL) { CFRelease(firstName); } //get Last Name CFStringRef lastName = (CFStringRef)ABRecordCopyValue(person,kABPersonLastNameProperty); contacts.lastNames = [(__bridge NSString*)lastName copy]; if (lastName != NULL) { CFRelease(lastName); } if (!contacts.firstNames) { contacts.firstNames = @""; } if (!contacts.lastNames) { contacts.lastNames = @""; } contacts.contactId = ABRecordGetRecordID(person); //append first name and last name contacts.fullname = [NSString stringWithFormat:@"%@ %@", contacts.firstNames, contacts.lastNames]; // get contacts picture, if pic doesn't exists, show standart one CFDataRef imgData = ABPersonCopyImageData(person); NSData *imageData = (__bridge NSData *)imgData; contacts.image = [UIImage imageWithData:imageData]; if (imgData != NULL) { CFRelease(imgData); } if (!contacts.image) { contacts.image = [UIImage imageNamed:@"avatar.png"]; } //get Phone Numbers NSMutableArray *phoneNumbers = [[NSMutableArray alloc] init]; ABMultiValueRef multiPhones = ABRecordCopyValue(person, kABPersonPhoneProperty); for(CFIndex i=0; i 

En realidad, recientemente escribí pod en Swift 3 usando CNContacts, a quien puede necesitar

Swift ContactBook Picker

No lo escribí yo mismo. Pero funciona conmigo:

 ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL); if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) { ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error) { ABAddressBookRef addressBook = ABAddressBookCreate( ); }); } else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) { CFErrorRef *error = NULL; ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error); CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook); CFIndex numberOfPeople = ABAddressBookGetPersonCount(addressBook); for(int i = 0; i < numberOfPeople; i++) { ABRecordRef person = CFArrayGetValueAtIndex( allPeople, i ); // NSString *firstName = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonFirstNameProperty)); // NSString *lastName = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonLastNameProperty)); // NSLog(@"Name:%@ %@", firstName, lastName); ABMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty); [[UIDevice currentDevice] name]; //NSLog(@"\n%@\n", [[UIDevice currentDevice] name]); for (CFIndex i = 0; i < ABMultiValueGetCount(phoneNumbers); i++) { NSString *phoneNumber = (__bridge_transfer NSString *) ABMultiValueCopyValueAtIndex(phoneNumbers, i); addressBookNum = [addressBookNum stringByAppendingFormat: @":%@",phoneNumber]; } } NSLog(@"AllNumber:%@",addressBookNum); } else { // Send an alert telling user to change privacy setting in settings app } 

y en mi .h

 @property NSString * addressBookNum;