纠结了好久的问题,分享给大家
项目需求:操作通讯录,当一个联系人中有多个手机号时,点击删除,删除对应的手机号;当剩下一个手机号时,点击删除,删除这个联系人,最后将这些修改保存到通讯录
ABMultiValueRemoveValueAndLabelAtIndex(phoneHaha, k);
昨天也用到这个方法,最后报错,也就没继续用这个方法,今天又试了下,把报错原因放百度里,第一个答案是stackovewflow上回答的,瞬间爱上了SO,
ABAddressBookRef
addressBook =
ABAddressBookCreate
();
NSArray *array = ( __bridge NSArray *) ABAddressBookCopyArrayOfAllPeople (addressBook);
for ( id obj in array) {
ABRecordRef people = ( __bridge ABRecordRef )obj;
ABMultiValueRef phone = ABRecordCopyValue (people, kABPersonPhoneProperty );
ABMutableMultiValueRef phoneHaha = ABMultiValueCreateMutableCopy (phone);
for ( int k = 0 ; k< ABMultiValueGetCount (phoneHaha); k++)
{
NSString * personPhone = ( __bridge NSString *) ABMultiValueCopyValueAtIndex (phoneHaha, k);
NSLog ( @"%@" ,personPhone);
if ([personPhone isEqualToString :[ self . phoneNumberArray objectAtIndex :button. tag ]]) {
if ( ABMultiValueGetCount (phoneHaha) > 1 ) {
ABMultiValueRemoveValueAndLabelAtIndex (phoneHaha, k);
ABRecordSetValue (people, kABPersonPhoneProperty , phoneHaha, nil );
} else {
ABAddressBookRemoveRecord (addressBook, people, NULL );
}
}
}
}
// 保存修改的通讯录对象
ABAddressBookSave (addressBook, NULL );
// 释放通讯录对象的内存
if (addressBook) {
CFRelease (addressBook);
NSArray *array = ( __bridge NSArray *) ABAddressBookCopyArrayOfAllPeople (addressBook);
for ( id obj in array) {
ABRecordRef people = ( __bridge ABRecordRef )obj;
ABMultiValueRef phone = ABRecordCopyValue (people, kABPersonPhoneProperty );
ABMutableMultiValueRef phoneHaha = ABMultiValueCreateMutableCopy (phone);
for ( int k = 0 ; k< ABMultiValueGetCount (phoneHaha); k++)
{
NSString * personPhone = ( __bridge NSString *) ABMultiValueCopyValueAtIndex (phoneHaha, k);
NSLog ( @"%@" ,personPhone);
if ([personPhone isEqualToString :[ self . phoneNumberArray objectAtIndex :button. tag ]]) {
if ( ABMultiValueGetCount (phoneHaha) > 1 ) {
ABMultiValueRemoveValueAndLabelAtIndex (phoneHaha, k);
ABRecordSetValue (people, kABPersonPhoneProperty , phoneHaha, nil );
} else {
ABAddressBookRemoveRecord (addressBook, people, NULL );
}
}
}
}
// 保存修改的通讯录对象
ABAddressBookSave (addressBook, NULL );
// 释放通讯录对象的内存
if (addressBook) {
CFRelease (addressBook);
}
完美解决这个问题,好开森