時得到EXC_BAD_ACCESS我爲得到通訊錄的聯繫人下面的方法:我打電話ABAddressBookRemoveRecord
- (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);
}
後來,在未來的觀點,我有一個選項,刪除選定的記錄&這是它的外觀像:
-(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 & ABRecordID傳遞到下一個看法,但在這兩種情況下,我不得不對EXC_BAD_ACCESS ABAddressBookRemoveRecord(地址簿,人,&錯誤) 。
兩行代碼都一樣 – 2013-02-02 12:03:32
@ShreeshGarg什麼?仔細觀察! – tipycalFlow 2013-05-16 15:47:34
對不起我的錯誤 – 2013-05-25 12:53:25