53529

how to edit a phone number values programmatically from address book ios

Question:

I'm trying to replace an specific phone number for an specific contact programmatically in iOS, taking the contacts form address book.

I don't know why I can't save the new phone number and refresh the address book to show the change.

I'm doing this:

+(BOOL) changeContactPhoneNumber:(NSString *) phoneSought forThis:(NSString *) newPhoneNumber{ ABAddressBookRef addressBook = ABAddressBookCreate(); ABRecordRef contactSelected; CFStringRef mobileLabelNumber; CFErrorRef error = nil; // Do whatever you want here. CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook); CFIndex nPeople = ABAddressBookGetPersonCount(addressBook); for (int i = 0; i < nPeople; i++) { ABRecordRef ref = CFArrayGetValueAtIndex(allPeople, i); ABMultiValueRef phones = (ABMultiValueRef)ABRecordCopyValue(ref, kABPersonPhoneProperty); NSString* mobilePhoneNumber=@""; if (ABMultiValueGetCount(phones) > 0) { for (int i=0; i < ABMultiValueGetCount(phones); i++) { [mobilePhoneNumber release]; mobilePhoneNumber = (NSString*)ABMultiValueCopyValueAtIndex(phones, i); if([mobilePhoneNumber isEqualToString:phoneSought]){ contactSelected = ref; mobileLabelNumber = ABMultiValueCopyLabelAtIndex(phones, i); } } } } ABMutableMultiValueRef phoneNumberMultiValue = ABMultiValueCreateMutable(kABPersonPhoneProperty); bool didAddPhone = ABMultiValueAddValueAndLabel(phoneNumberMultiValue ,(__bridge CFTypeRef)newPhoneNumber,mobileLabelNumber, NULL); if(didAddPhone){ ABRecordSetValue(ABAddressBookGetPersonWithRecordID(addressBook, contactSelected), kABPersonPhoneProperty, phoneNumberMultiValue, nil); bool bSuccess = ABAddressBookSave(addressBook, &error); if (!bSuccess) { NSLog(@"Could not save to address book: %@", error); } else { return YES; } } else { NSLog(@"Error editing phone number: %@", error); error = nil; } return NO; }

Answer1:

You should debug your code and try to figure out whether the format of the phone numbers you are providing to the method are matching or not.

For e.g. when i am logging my contact list phone numbers these are results

Number...555-478-7672 Number...(408) 439-5270 Number...(408) 555-3514 Number...888-555-5512 Number...888-555-1212 Number...555-522-8243 Number...(555) 766-4823 Number...(707) 555-1854 Number...555-610-6679

And i was comparing these number against unformatted number string.

Secondly

ABRecordSetValue(ABAddressBookGetPersonWithRecordID(addressBook, contactSelected), kABPersonPhoneProperty, phoneNumberMultiValue, nil);

Whose actual declaration is

ABRecordSetValue(ABRecordRef record, ABPropertyID property, CFTypeRef value, CFErrorRef* error);

Although ABAddressBookGetPersonWithRecordID returns a ABRecordRef but you already have ABRecordRef contactSelected; so in my view you should use

ABRecordSetValue(contactSelected,kABPersonPhoneProperty,phoneNumberMultiValue,nil);

Please correct me if i am wrong or have misunderstood your code!

Recommend

  • How to get Custom label and value of the Email in ABAddressBook
  • Creating an immutable CFArray object - Apple sample doesn't work
  • Potential Memory leak abaddressbookcopyarrayofallpeople( )
  • CF10 Fieldboost on cfindex has no effect
  • how to get contacts in to our native application from Addressbook
  • Swift Compile Error Bitcast requires both operands to be pointer or neither
  • Fetch all email-id of contact list of iphone in to the iphone app
  • iPhone DNS resolution, not all A records returned
  • Potential Memory Leak for ABRecordCopyValue
  • Varying content and presentation by host name in Sitecore 6.4
  • scaml “illegal nesting” error
  • has been compiled by a more recent version of the Java Runtime (class file version 53.0)
  • Sencha Cmd broken: Why does 'app build' ignore any command line parameters?
  • How to get PHP working in Brackets?
  • jQuery doesn't always load
  • Navigation Buttons no whitespace underneathe
  • Access Amazon s3 using http in angular2
  • On Index page, when editing 1 row, all rows are re-rendering (painfully slow). Using Reactjs with Ru
  • UWP Template 10 create a dynamic hamburgermenu
  • Sitecore 8: Number of Recipients in List Manager not calculating properly
  • Horizontal Menu Displays Incorrect on Widescreen Monitor
  • Retrieve google contact based on contact Id
  • Send money from Braintree
  • jQuery, Calling multiple animations in a row on click
  • Symfony : Pagination with inner join
  • XMPPConnection Error in android
  • Could not install package 'Microsoft.Owin.Security 2.0.1'
  • How to force refresh on CallLog.Calls.CACHED_NAME column?
  • Submission of new app with iAds
  • testing a POST using phpunit in laravel 4
  • How to remove a SwiftyJSON element?
  • Row Count Is Returning the incorrect number using RaptureXML
  • Obtain ObjectIdHex value from mgo query
  • what is the difference between the asp.net mvc application and asp.net web application
  • NSLayoutConstraint that would pin a view to the bottom edge of a superview
  • Hits per day in Google Big Query
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Linking SubReports Without LinkChild/LinkMaster
  • Bitwise OR returns boolean when one of operands is nil
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass