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.
Tags: ABMultiValueAddValueAndLabel, ABMutableMultiValueRef, kABHomeLabel, kABPersonPhoneMobileLabel, kABPersonPhoneProperty, phoneNumberMultiValue
March 9, 2009 at 2:58 am |
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
May 6, 2009 at 1:42 am |
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!
May 6, 2009 at 1:45 am |
[...] Adding phonenumbers to ABPerson Adding phonenumbers to ABPerson [...]
September 30, 2009 at 9:44 pm |
How do add a custom label to a contact. Do you have an example of that please
October 1, 2009 at 11:29 am |
I’m sorry i don’t
October 9, 2009 at 2:17 pm |
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);
October 20, 2009 at 1:02 pm |
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.
October 22, 2009 at 8:28 pm |
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.
October 23, 2009 at 1:20 pm |
Np. Guess you could do that too