Chandresh Kachariya
Chandresh Kachariya

Reputation: 677

Update contact name in address book programmatically?

I get a success message, contact is successfully updated but in contact application not display updated name.

I have call below method for updating my contact name.

-(void)updateAddressBook
{
    CFErrorRef *error = NULL;

    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL,error);

    ABRecordRef source = ABAddressBookCopyDefaultSource(addressBook);
    CFArrayRef sortedPeople =ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(addressBook, source, kABPersonSortByFirstName);

    CFIndex number = CFArrayGetCount(sortedPeople);

    NSString *firstName;
    NSString *phoneNumber ;

    for( int i=0;i<number;i++)
    {
        ABRecordRef person = CFArrayGetValueAtIndex(sortedPeople, i);

        firstName = (__bridge NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
        ABMultiValueRef phones = ABRecordCopyValue(person, kABPersonPhoneProperty);
        phoneNumber = (__bridge NSString *) ABMultiValueCopyValueAtIndex(phones, 0);

        ABRecordID recordID1 = ABRecordGetRecordID(person);
        NSLog(@"%d", recordID1);

        ABRecordRef record = ABAddressBookGetPersonWithRecordID(addressBook, recordID1);
        NSNumber *recordId = [NSNumber numberWithInteger:ABRecordGetRecordID(record)];
        NSLog(@"recordId:- %d",[recordId intValue]);

        bool didSet;

        //update First name
        didSet = ABRecordSetValue(record, kABPersonFirstNameProperty, (__bridge CFTypeRef)([NSString stringWithFormat:@"Person Name"]) , nil);
        if (didSet) {
            NSLog(@"contact is successfully updated");
        }else{
            NSLog(@"error");
        }

        CFStringRef firstName;
        firstName = ABRecordCopyValue(person, kABPersonFirstNameProperty);
        NSLog(@"%@",firstName);

        ABAddressBookSave(addressBook, error)
    }
}

Upvotes: 0

Views: 173

Answers (2)

vishnu varthan
vishnu varthan

Reputation: 21

-(void)showPersonViewController:(NSString *)nameInContact
{

// Fetch the address book 

ABAddressBookRef addressBook = ABAddressBookCreate();

// Search for the person  in the address book



NSArray *people = (NSArray*)ABAddressBookCopyPeopleWithName(addressBook, CFSTR(nameInContact));

// Display the information if found in the address book 

if ((people != nil) && [people count])
{
ABRecordRef person = (ABRecordRef)[people objectAtIndex:0];
ABPersonViewController *picker = [[[ABPersonViewController alloc] init] autorelease];
    picker.personViewDelegate = self;
    picker.displayedPerson = person;

// Allow users to edit the person’s information

    picker.allowsEditing = YES;
    [self.navigationController pushViewController:picker animated:YES];
}
else 
{
 // Show an alert if the person is not in Contacts



 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:[NSString stringWithFormat:@"Could not find %@ in the Contacts application", nameInContact] delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
 [alert show];
 [alert release];
}

[people release];
CFRelease(addressBook);
}

Upvotes: 0

kinshukkar
kinshukkar

Reputation: 158

You have to do a ABAddressBookSave to commit the changes.

Try the following -

ABAddressBookSave(addressBook, error)

Upvotes: 1

Related Questions