2016-10-06 8 views
0

私は成功メッセージを受け取り、連絡先は正常に更新されますが、連絡先アプリケーションには更新された名前は表示されません。アドレス帳の連絡先の名前をプログラムで更新しますか?

連絡先の名前を更新する方法は以下のとおりです。

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

いずれかがありますか?私はあなたの答えを楽しみにしています!

+0

あなたの応答のためのhttp://www.theappguruz.com/blog/ios-manage-contacts-from-your-application –

+0

おかげ@Jeckyをこのデモを試してみてください私はABPersonViewControllerを開かずに連絡先を編集しました –

+0

これを試してくださいhttp://stackoverflow.com/questions/7577005/how-to-modify-a-contact-number-in-address-book-programatically – sandy

答えて

1

変更をコミットするには、ABAddressBookSaveを実行する必要があります。

次のことを試してみてください -

ABAddressBookSave(addressBook, error) 
+1

ありがとうございました@kinshukkarそれは私のために働いた –

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); 
} 
+0

https://www.appcoda.com/ios-programming-import-contact-address-book/ –

+0

私はABPersonViewCを使いたくないオンロード –

関連する問題