公告
财富商城
积分规则
提问
发文
2020-02-15 05:13发布
Root(大扎)
I want to delete a contact from the address book in iPhone how can we do that ?
Thanks
+(BOOL)removedRecordFromAddressBookWithFirstValue:(NSString *)fullName{ BOOL recordRemoved=NO; CFErrorRef err; ABAddressBookRef addressBook= ABAddressBookCreateWithOptions(NULL,&err); CFArrayRef people=ABAddressBookCopyArrayOfAllPeople(addressBook); CFIndex nPeople=ABAddressBookGetPersonCount(addressBook); NSString *currRecordFullName=[[NSString alloc] init]; /*Invariant: No record with the name fullName has been found so far.*/ for(int i=0;i<nPeople;i++){ ABRecordRef ref=CFArrayGetValueAtIndex(people,i); CFErrorRef error=NULL; currRecordFullName=nil; currRecordFullName=[[NSString alloc] init]; currRecordFullName= (__bridge NSString *)ABRecordCopyCompositeName(ref); if([currRecordFullName isEqualToString:fullName]){ /*The record to be deleted has been found.*/ ABAddressBookRemoveRecord(addressBook,ref,&error); ABAddressBookSave(addressBook,&error); CFRelease(ref); CFRelease(addressBook); if(error!=NULL){ CFStringRef errorDesc=CFErrorCopyDescription(error); NSLog(@"Failed to remove record: %@",errorDesc); CFRelease(errorDesc); }else{ NSLog(@"Record removed"); recordRemoved=YES; } break; }else{;} } return recordRemoved; }
ABAddressBookRemoveRecord
http://developer.apple.com/iphone/library/documentation/AddressBook/Reference/ABAddressBookRef_iPhoneOS/Reference/reference.html#//apple_ref/c/func/ABAddressBookRemoveRecord
Here is a sample code to do the same:
- (void)delAllContacts { ABAddressBookRef addressBook = CFBridgingRetain((__bridge id)(ABAddressBookCreateWithOptions(NULL, NULL))); int count = ABAddressBookGetPersonCount(addressBook); if(count==0 && addressBook!=NULL) { //If there are no contacts, don't delete CFRelease(addressBook); return; } //Get all contacts and store it in a CFArrayRef CFArrayRef theArray = ABAddressBookCopyArrayOfAllPeople(addressBook); for(CFIndex i=0;i<count;i++) { ABRecordRef person = CFArrayGetValueAtIndex(theArray, i); //Get the ABRecord BOOL result = ABAddressBookRemoveRecord (addressBook,person,NULL); //remove it if(result==YES) { //if successful removal BOOL save = ABAddressBookSave(addressBook, NULL); //save address book state if(save==YES && person!=NULL) { CFRelease(person); } else { NSLog(@"Couldn't save, breaking out"); break; } } else { NSLog(@"Couldn't delete, breaking out"); break; } } if(addressBook!=NULL) { CFRelease(addressBook); } }
最多设置5个标签!
ABAddressBookRemoveRecord
http://developer.apple.com/iphone/library/documentation/AddressBook/Reference/ABAddressBookRef_iPhoneOS/Reference/reference.html#//apple_ref/c/func/ABAddressBookRemoveRecord
Here is a sample code to do the same: