iphone - 地址簿:尝试添加 'Home' 和 'Work' 地址,但只显示 1 个

标签 iphone objective-c addressbook

我正尝试在个人记录中添加“家庭”和“工作”地址。似乎只有 1 个出现(后来添加的那个。是否可以向一个人添加多个地址并在 UnknownPersonViewController 中看到它们显示?如果是这样,我应该怎么做?

这是我的代码:

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);
}     

最佳答案

您要做的是对两个地址使用相同的可变 ABMultiValueRef:

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);

关于iphone - 地址簿:尝试添加 'Home' 和 'Work' 地址,但只显示 1 个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3458590/

相关文章:

iphone - UITableview背景 View 颜色-iphone

ios - 在使用 MKMapview 导致 MB 的内存使用和使用 ARC 时,当 View 消失时它不会释放内存

ios - 如何清空 UIPageViewController(移除里面的 ViewControllers)

ios - 将字符串从 TableView Controller 传递到新 View Controller

xcode - 如何检索用户联系人并将其显示在 UITableView 中?

iphone - 如何让UIButton背景透明?

ios - 从包含 User 类对象的 NSMutableArray 获取基于属性的唯一对象

iphone - 从弹出 View 导航到另一个 View Controller

ios 6.0 新地址簿 ABAddressBookRequestAccessWithCompletion

iphone - iPhone 中的地址簿实现