2016-10-06 2 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를 사용하고 싶지 않습니다. 점원 –

관련 문제