Friday, April 27, 2012

How to append phone number to an existing contact.


    //We need to create a mutable copy of ABMultiValueRef with property kABPersonPhoneProperty
    
    ABMultiValueRef Phones = ABRecordCopyValue(contact, kABPersonPhoneProperty);
    ABMutableMultiValueRef Phone = ABMultiValueCreateMutableCopy(Phones);
    ABMultiValueAddValueAndLabel(Phone,@"413-675-1234", kABOtherLabel, NULL);
    ABMultiValueAddValueAndLabel(Phone@"413-675-1235", kABOtherLabel, NULL); 
    ABRecordSetValue(contact, kABPersonPhonePropertyPhone,nil);
    CFRelease(Phone);

    //This will add 2 numbers with others title under phone section of contact.
    //You can use other label too like following kABWorkLabel or kABPersonPhoneMobileLabel 

    ABMultiValueAddValueAndLabel(Phone,@"413-675-1234"kABPersonPhoneMobileLabelNULL);
    ABMultiValueAddValueAndLabel(Phone@"413-675-1235"kABWorkLabelNULL); 


    //Dont forget to save your addressbook after that.

    ABAddressBookSave(addressbook, &error);
    if (error != NULL
    {
        NSLog(@"failed");

    }
      NSLog(@"success.");

Note: If you don't make a  mutable copy, you will get following error for trying add  phone number to ABMultiValueRef instead of ABMutableMultiValueRef. As you created ABMultiValueRef by copying from ABRecordRef using method ABRecordCopyValue.
Assertion failed: (((ABCMultiValue *)multiValue)->flags.isMutable), function ABMultiValueAddValueAndLabel





No comments:

Post a Comment