4

I'm trying to add a 'Home' and 'Work' address my Person record. It seems only 1 shows up (the one added later. Is if possible to add multiple addresses to a Person and see them displayed in the UnknownPersonViewController? If so, how should I do this?

Here's my code:

void multiValueAddDictionaryValueAndLabel(ABMultiValueRef multi, CFDictionaryRef values, CFStringRef label) {
    if (multi && values != NULL) {
       ABMultiValueAddValueAndLabel(multi, values, label, NULL);          
    }               
}

CFStringRef getValueForKey(CFDictionaryRef dict, CFStringRef key) {
   CFStringRef value = NULL;

   if (CFDictionaryContainsKey(dict, key)) {
      value = CFDictionaryGetValue(dict, key);
   }

   return value;
}

ABRecordRef createPerson(CFDictionaryRef dict) {
   ABRecordRef person = ABPersonCreate();

   /*
    Add work address ...
    */

   ABMultiValueRef workAddress = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);
   NSDictionary *values = [NSDictionary dictionaryWithObjectsAndKeys:
                               (NSString *)getValueForKey(dict, CFSTR("d:street")), (NSString *)kABPersonAddressStreetKey,
                               (NSString *)getValueForKey(dict, CFSTR("d:postalcode")), (NSString *)kABPersonAddressZIPKey,
                               (NSString *)getValueForKey(dict, CFSTR("d:l")), (NSString *)kABPersonAddressCityKey,
                               (NSString *)getValueForKey(dict, CFSTR("d:st")), (NSString *)kABPersonAddressCityKey,
                               (NSString *)getValueForKey(dict, CFSTR("d:co")), (NSString *)kABPersonAddressCountryKey,
                               nil];
   multiValueAddDictionaryValueAndLabel(workAddress, (CFDictionaryRef)values, kABWorkLabel);
   ABRecordSetValue(person, kABPersonAddressProperty, workAddress, NULL);
   CFRelease(workAddress);

   /*
    Add home address ...
    */

   ABMultiValueRef homeAddress = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);
 values = [NSDictionary dictionaryWithObjectsAndKeys:
             (NSString *)getValueForKey(dict, CFSTR("d:homeStreet")), (NSString *)kABPersonAddressStreetKey,
             (NSString *)getValueForKey(dict, CFSTR("d:homePostalCode")), (NSString *)kABPersonAddressZIPKey,
             (NSString *)getValueForKey(dict, CFSTR("d:homeCity")), (NSString *)kABPersonAddressCityKey,
             (NSString *)getValueForKey(dict, CFSTR("d:homeState")), (NSString *)kABPersonAddressCityKey,
             (NSString *)getValueForKey(dict, CFSTR("d:homeCountry")), (NSString *)kABPersonAddressCountryKey,
             nil];
   multiValueAddDictionaryValueAndLabel(homeAddress, (CFDictionaryRef)values, kABHomeLabel);
   ABRecordSetValue(person, kABPersonAddressProperty, homeAddress, NULL);
   CFRelease(homeAddress);
}     
Wolfgang Schreurs
  • 11,779
  • 7
  • 51
  • 92

2 Answers2

6

What you want to do is use the same mutable ABMultiValueRef for both addresses:

ABMultiValueRef addresses = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);

// set up your 2 dictionaries here as you did in your question (though obviously with differing names)

ABMultiValueAddDictionaryValueAndLabel(addresses, (CFDictionaryRef)workValues, kABWorkLabel);
ABMultiValueAddDictionaryValueAndLabel(addresses, (CFDictionaryRef)homeValues, kABHomeLabel);

ABRecordSetValue(person, kABPersonAddressProperty, homeAddress, NULL);
CFRelease(addresses);
Wevah
  • 28,182
  • 7
  • 83
  • 72
  • Thanks - just figured it out myself a sec ago and was about to post my solution :) Anyways, for my improved code, look below. – Wolfgang Schreurs Aug 11 '10 at 13:29
  • Sweet; glad you were able to figure it out! – Wevah Aug 11 '10 at 13:33
  • 2
    There is no ABMultiValueAddDictionaryValueAndLabel function in the iOS address book API. The example above should be ABMultiValueAddValueAndLabel(addresses, (CFDictionaryRef)workValues, kABWorkLabel, NULL); – Christopher Pickslay Dec 21 '10 at 21:15
  • Hm; I wonder where I got that function from? It's not in the OS X docs either (it should be `ABMultiValueAdd()`). I must have mistakenly inferred it from the OP… – Wevah Dec 21 '10 at 21:21
1

This code works:

ABMultiValueRef addresses = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);

values = [NSDictionary dictionaryWithObjectsAndKeys:
                        (NSString *)getValueForKey(dict, CFSTR("d:street")), (NSString *)kABPersonAddressStreetKey,
                        (NSString *)getValueForKey(dict, CFSTR("d:postalcode")), (NSString *)kABPersonAddressZIPKey,
                        (NSString *)getValueForKey(dict, CFSTR("d:l")), (NSString *)kABPersonAddressCityKey,
                        (NSString *)getValueForKey(dict, CFSTR("d:st")), (NSString *)kABPersonAddressCityKey,
                        (NSString *)getValueForKey(dict, CFSTR("d:co")), (NSString *)kABPersonAddressCountryKey,
                        nil];
multiValueAddDictionaryValueAndLabel(addresses, (CFDictionaryRef)values, kABWorkLabel);

values = [NSDictionary dictionaryWithObjectsAndKeys:
          (NSString *)getValueForKey(dict, CFSTR("d:homeStreet")), (NSString *)kABPersonAddressStreetKey,
          (NSString *)getValueForKey(dict, CFSTR("d:homePostalCode")), (NSString *)kABPersonAddressZIPKey,
          (NSString *)getValueForKey(dict, CFSTR("d:homeCity")), (NSString *)kABPersonAddressCityKey,
          (NSString *)getValueForKey(dict, CFSTR("d:homeState")), (NSString *)kABPersonAddressCityKey,
          (NSString *)getValueForKey(dict, CFSTR("d:homeCountry")), (NSString *)kABPersonAddressCountryKey,
          nil];
multiValueAddDictionaryValueAndLabel(addresses, (CFDictionaryRef)values, kABHomeLabel);

ABRecordSetValue(person, kABPersonAddressProperty, addresses, NULL);
CFRelease(addresses);
Wolfgang Schreurs
  • 11,779
  • 7
  • 51
  • 92