Adding a New Number to an Existing ABRecord in ABAddressBook - iPhone

I am trying to update the contents of an existing contact in the address book through my application, but without using the user interface. The scenario is as follows:

1 The user enters the number and name 2 The application checks whether this name is in the contact list 3 if he then checks whether the number is one of the contacts for this name 4 If it is not added to this name

I was able to follow steps 1-3, but I could not find a way to do 4. Can someone help?

Below if my code looks like

... CFIndex lTotalContactsCount = ABAddressBookGetPersonCount(lAddressBook); NSArray *people = (NSArray *)ABAddressBookCopyArrayOfAllPeople(lAddressBook ); for (CFIndex i = 0; i < lTotalContactsCount; i++) { ABRecordRef lRef = (ABRecordRef)[people objectAtIndex:i]; ... // if names match { ABMutableMultiValueRef lPhoneNumbers = ABRecordCopyValue(lRef, kABPersonPhoneProperty); CFIndex lContactPhoneNumberCount = ABMultiValueGetCount(lPhoneNumbers); ABRecordID contactID = ABRecordGetRecordID(lRef); ... // if numbers dont match { // THIS BIT IS NOT WOKRING CFErrorRef error = NULL; ABMutableMultiValueRef multiPhone = ABMultiValueCreateMutable(kABMultiStringPropertyType); ABMultiValueAddValueAndLabel(multiPhone, number, (CFStringRef)@"Duplicate", NULL); // ABRecordSetValue(newPerson, kABPersonFirstNameProperty, name, &error); //add the number to the contact ABRecordSetValue(lRef, kABPersonPhoneProperty, multiPhone,nil); // ABAddressBookAddRecord(lAddressBook, lRef, &error); ABAddressBookSave(lAddressBook, &error); } if( firstName ) CFRelease(firstName); if( lastName ) CFRelease(lastName); if( lPhoneNumbers ) CFRelease(lPhoneNumbers); // no need to search other entries if(numberExists) break; } 
+4
source share
1 answer

After tomorrow's look in the APIs, I managed to find a solution. Here you are:

 // contactId is the ID of the person i need to add a new number to his contacts // got the id through : ABRecordGetRecordID( ABRecordRef ) ABRecordRef person = ABAddressBookGetPersonWithRecordID(lAddressBook, contactID); ABMutableMultiValueRef multiPhone = ABMultiValueCreateMutableCopy(lPhoneNumbers); ABMultiValueAddValueAndLabel(multiPhone, number, (CFStringRef)@"Duplicate", NULL); //add the number to the contact ABRecordSetValue(person, kABPersonPhoneProperty, multiPhone,nil); ABAddressBookSave(lAddressBook, &error); 
+7
source

Source: https://habr.com/ru/post/1336195/


All Articles