How to delete a contact from address book in iPhone using objective-c?

后端 未结 3 604
情话喂你
情话喂你 2020-12-22 01:24

I want to delete a contact from the address book in iPhone how can we do that ?

Thanks

相关标签:
3条回答
  • 2020-12-22 01:37

    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);
        }
    }
    
    0 讨论(0)
  • 2020-12-22 01:56
    +(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;
    }
    
    0 讨论(0)
  • 2020-12-22 01:57

    ABAddressBookRemoveRecord

    http://developer.apple.com/iphone/library/documentation/AddressBook/Reference/ABAddressBookRef_iPhoneOS/Reference/reference.html#//apple_ref/c/func/ABAddressBookRemoveRecord

    0 讨论(0)
提交回复
热议问题