纠结了好久的问题,分享给大家
项目需求:操作通讯录,当一个联系人中有多个手机号时,点击删除,删除对应的手机号;当剩下一个手机号时,点击删除,删除这个联系人,最后将这些修改保存到通讯录
ABMultiValueRemoveValueAndLabelAtIndex(phoneHaha, k);
昨天也用到这个方法,最后报错,也就没继续用这个方法,今天又试了下,把报错原因放百度里,第一个答案是stackovewflow上回答的,瞬间爱上了SO,
代码如下:
ABAddressBookRefaddressBook =ABAddressBookCreate();NSArray *array = (__bridgeNSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);for (idobj in array) {ABRecordRef people = (__bridgeABRecordRef)obj;ABMultiValueRef phone = ABRecordCopyValue(people,kABPersonPhoneProperty);ABMutableMultiValueRef phoneHaha = ABMultiValueCreateMutableCopy(phone);for (intk = 0; k<ABMultiValueGetCount(phoneHaha); k++){NSString * personPhone = (__bridgeNSString*)ABMultiValueCopyValueAtIndex(phoneHaha, k);NSLog(@"%@",personPhone);if ([personPhoneisEqualToString:[self.phoneNumberArrayobjectAtIndex: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);}
完美解决
如果觉得《iOS 通讯录操作 删除联系人 以及联系人多个号码 如何删除其中一个号码》对你有帮助,请点赞、收藏,并留下你的观点哦!