Working with iPhone contacts: Adding phonenumbers to ABPerson

So.. after figuring out how to store a contact in the iPhone addressbook, using the AddressBook framework of the iPhone SDK, i realized that storing phonenumbers wasent quite as straight forward as storing fields like first- and lastname.

Phonenumbers are stored in a “multivalue” property. This means that multiple values with different “labels” can be stored for the property “phone”  (kABPersonPhoneProperty) of a person.

Anyways – i thought i would throw in the code:

ABMutableMultiValueRef phoneNumberMultiValue =  ABMultiValueCreateMutable(kABPersonPhoneProperty);  

ABMultiValueAddValueAndLabel(phoneNumberMultiValue, @"+45 10 20 30 40",  kABHomeLabel, NULL);

ABMultiValueAddValueAndLabel(phoneNumberMultiValue, @"+45 10 20 30 40",  kABPersonPhoneMobileLabel, NULL);

ABRecordSetValue(person, kABPersonPhoneProperty, phoneNumberMultiValue, nil); 

Again – it should be pretty self explanatory. First a new MutableMultiValue is created. Then two labels with values (phonenumbers) are added to the multivalue, and then the multivalue is added to the person (see my previous post, for how to create and store a person).

Notice that the constant for the “Mobil” phonenumber label is called kABPersonPhoneMobileLabel – kABPersonPhone***** is what most of the phonenumber label constants are called. But “Home” and “Work” are just called “kABHomeLabel” and “kABWorkLabel”. I guess these can be used for identifying other stuff like emails and addresses.

Advertisements

Tags: , , , , ,

9 Responses to “Working with iPhone contacts: Adding phonenumbers to ABPerson”

  1. Gianluca Says:

    Hello,

    thanks for the post, actually I was trying to read phone numbers from the address book, do you have an example for that ?

    I would like to build an application that can open the address book and let the user choose a phone number to display in my application.

    Thanks!

    GL

  2. bearc0025 Says:

    Looks like email is the same way:

    ABMutableMultiValueRef emailMultiValue = ABMultiValueCreateMutable(kABPersonEmailProperty);
    ABMultiValueAddValueAndLabel(emailMultiValue, email, kABOtherLabel, NULL);
    ABRecordSetValue(person, kABPersonEmailProperty, emailMultiValue, nil);

    Where email is the string and kABOtherLabel cd also be kABHomeLabel or kABWorkLabel.

    Thanks!

  3. Adding phonenumbers to ABPerson « Brainwash Inc. - iPhone/Mobile Development Says:

    […] Adding phonenumbers to ABPerson Adding phonenumbers to ABPerson […]

  4. David Says:

    How do add a custom label to a contact. Do you have an example of that please

  5. am Says:

    Nice article, thx.
    I just tried it out and ran into a problem. If I try to add for example a new URL to an existing contact the old URL(s) were replaced by the new one. Isn’t _ABMultiValueAddValueAndLabel_ supposed to _ADD_ a new value? Instead it deletes the complete content and inserts the new URL.

    Anyone else experienced this issue? Am I missing something?


    ABAddressBookRef addressBook = ABAddressBookCreate();
    ABRecordRef person = ABAddressBookGetPersonWithRecordID(addressBook, personID);
    ABMutableMultiValueRef theUrls = ABMultiValueCreateMutable(kABMultiStringPropertyType);

    ABMultiValueAddValueAndLabel(theUrls, newUrl, kABOtherLabel, NULL);
    ABRecordSetValue(person, kABPersonURLProperty, theUrls, &retVal);
    ABAddressBookSave(addressBook, &retVal);

  6. Olav Rask Says:

    AM ->

    I would think you would have to obtain an reference to the existing ABMultiValue stored in the persons URLProperty and add you new url to this.

    ABRecordSetValue(person, kABPersonURLProperty, theUrls, &retVal); sets an entire new collection of urls, containing only your new url.

  7. am Says:

    Thx for replying. I solved the Problem in the meantime. I had to iterate through the URLs … get all the Field values and put them together with the new URL back as a multivalue property into the addressbook.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: