0

I have the following method for getting the AddressBook contacts:

- (void) scanAddressBook
{

NSUInteger i;
NSUInteger k;

ABAddressBookRef addressBook = ABAddressBookCreate();
NSArray *people = (__bridge_transfer NSArray *) ABAddressBookCopyArrayOfAllPeople(addressBook);

if ( people==nil )
{
    NSLog(@"NO ADDRESS BOOK ENTRIES TO SCAN");
    CFRelease(addressBook);
    return;
}

for ( i=0; i<[people count]; i++ )
{
    ABRecordRef person = (__bridge ABRecordRef)[people objectAtIndex:i];
    NSNumber *recordID = [NSNumber numberWithUnsignedInt:ABRecordGetRecordID(person)];

    ABMutableMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty);
    NSString *name = [NSString stringWithFormat:@"%@ %@",
                      (__bridge_transfer NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty),
                      (__bridge_transfer NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty)];



    CFIndex phoneNumberCount = ABMultiValueGetCount( phoneNumbers );
    NSMutableArray *arrPhoneNumbers = [[NSMutableArray alloc] initWithCapacity:phoneNumberCount];

    for ( k=0; k<phoneNumberCount; k++ )
    {
        CFStringRef phoneNumberLabel = ABMultiValueCopyLabelAtIndex( phoneNumbers, k );
        NSString *phoneNumberValue = (__bridge_transfer NSString *)ABMultiValueCopyValueAtIndex( phoneNumbers, k );
        CFStringRef phoneNumberLocalizedLabel = ABAddressBookCopyLocalizedLabel( phoneNumberLabel );


        [arrPhoneNumbers addObject:phoneNumberValue];

        CFRelease(phoneNumberLocalizedLabel);
        CFRelease(phoneNumberLabel);
    }

    NSMutableDictionary *dictPerson = [[NSMutableDictionary alloc] init];
    [dictPerson setObject:name forKey:@"contactName"];
    [dictPerson setObject:arrPhoneNumbers  forKey:@"phoneNumbers"];
    [dictPerson setObject:recordID forKey:@"recordID"];

    [arrABFriends addObject:dictPerson];
}

CFRelease(addressBook);
}

Later on, in the next view, I have an option to delete the selected record & this is how it looks like:

-(IBAction) removeContactFromAB
{
ABAddressBookRef addressBook; 
CFErrorRef error = NULL; 
addressBook = ABAddressBookCreate(); //
CFRetain(addressBook);
ABRecordID recordID = (ABRecordID)[[personToDump objectForKey:@"recordID"] integerValue];
NSLog(@"recordID: %d", recordID);
//    ABRecordRef person = ABAddressBookGetPersonWithRecordID(addressBook,(ABRecordID)recordID);
ABRecordRef person = ABAddressBookGetGroupWithRecordID(addressBook, recordID);
//fullName.text = (NSString *)ABRecordCopyCompositeName((ABRecordRef)person);

ABAddressBookRemoveRecord( addressBook, person, &error );

if(error !=NULL)
{
    UIAlertView    *alert =[[UIAlertView alloc] initWithTitle:@"error" message:@"deleting" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"OK",nil];
    [alert show];        
}

ABAddressBookSave(addressBook, &error);
NSLog(@"ERROR: %@", &error);
CFRelease(addressBook);

}

I tried to pass the ABRecordRef & ABRecordID to the next view but in both cases I had EXC_BAD_ACCESS on ABAddressBookRemoveRecord( addressBook, person, &error ).

Kex
  • 776
  • 10
  • 22

2 Answers2

2

Did your ABAddressBookGetGroupWithRecordID(addressBook, recordID) return something other than null?

I'm having the same problem, i think and the problem is somewhere around the conversation of the recordID's. If you try

NSNumber *recordID=[NSNumber numberWithUnsignedInt:ABRecordGetRecordID(person)];
ABRecordID recordID2 =(ABRecordID) recordID2; 
ABAddressBookGetGroupWithRecordID(addressBook, recordID2)

it's going to return null as well. I'm not sure how to solve this yet.

ui.: I wanted to add this in comment but turns out i dont have enough rep yet:(

Hooloovoo
  • 86
  • 4
  • Try changing this line: NSNumber *recordID = [NSNumber numberWithUnsignedInt:ABRecordGetRecordID(person)]; to NSString *recordID = [NSString stringWithFormat:@"%d",ABRecordGetRecordID(person)]]; where You initialize the groups. It should solve the problem. – Hooloovoo Mar 27 '12 at 09:09
  • a better solution: http://stackoverflow.com/questions/2138923/iphone-addressbook-getting-null-item-in-abaddressbookgetpersonwithrecordid – Hooloovoo Mar 27 '12 at 09:24
1

The problem was that I was trying to retrieve the reference from a group with an ID from a reference & I was getting nil as a reference & later trying to find the record with the reference nil. That's where my App was giving EXC_BAD_ACCESS.

So instead of:

ABRecordRef person = ABAddressBookGetGroupWithRecordID(addressBook, recordID);

There should be:

ABRecordRef person = ABAddressBookGetPersonWithRecordID(addressBook, recordID);
Kex
  • 776
  • 10
  • 22