以编程方式更新地址簿中的联系人姓名?

时间:2016-10-06 06:54:54

标签: ios objective-c iphone

我收到成功消息,联系人已成功更新,但联系人应用程序中未显示更新名称。

我在下方调用了更新联系人姓名的方法。

-(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)
    }
}

2 个答案:

答案 0 :(得分:1)

您必须执行ABAddressBookSave才能提交更改。

尝试以下方法 -

ABAddressBookSave(addressBook, error)

答案 1 :(得分:0)

-(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);
}
相关问题