调用ABAddressBookRemoveRecord时,我得到EXC_BAD_ACCESS

时间:2012-03-23 17:57:45

标签: objective-c ios xcode exc-bad-access abaddressbook

我有以下方法来获取AddressBook联系人:

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

稍后,在下一个视图中,我可以选择删除所选记录&amp;这是它的样子:

-(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);

}

我试图通过ABRecordRef&amp; ABRecordID到下一个视图,但在两种情况下我都在ABAddressBookRemoveRecord上有EXC_BAD_ACCESS(addressBook,person,&amp; error)。

2 个答案:

答案 0 :(得分:2)

您的ABAddressBookGetGroupWithRecordID(addressBook,recordID)是否返回了null以外的内容?

我遇到了同样的问题,我认为问题出在了recordID的谈话中。 如果你试试

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

它也将返回null。 我不知道如何解决这个问题。

ui。:我想在评论中加上这个,但事实证明我还没有足够的代表:(

答案 1 :(得分:1)

问题在于我试图从具有来自参考&amp;的ID的组中检索引用。我作为参考&amp;后来试图找到参考为零的记录。这就是我的应用程序提供EXC_BAD_ACCESS的地方。

所以而不是:

ABRecordRef person = ABAddressBookGetGroupWithRecordID(addressBook, recordID);

应该有:

ABRecordRef person = ABAddressBookGetPersonWithRecordID(addressBook, recordID);