【问题标题】:AddressBook: trying to add 'Home' and 'Work' addresses, only 1 shows up地址簿:尝试添加“家庭”和“工作”地址,仅显示 1 个
【发布时间】:2010-08-11 13:10:31
【问题描述】:

我正在尝试在我的个人记录中添加“家庭”和“工作”地址。似乎只显示了 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);
}     

【问题讨论】:

    标签: iphone objective-c addressbook


    【解决方案1】:

    您要做的是对两个地址使用相同的可变 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);
    

    【讨论】:

    • 谢谢 - 一秒钟前我自己想通了,正要发布我的解决方案:) 不管怎样,对于我改进的代码,请看下面。
    • 甜的;很高兴你能弄明白!
    • iOS通讯录API中没有ABMultiValueAddDictionaryValueAndLabel函数。上面的例子应该是 ABMultiValueAddValueAndLabel(addresses, (CFDictionaryRef)workValues, kABWorkLabel, NULL);
    • 嗯;我想知道我从哪里得到这个功能?它也不在 OS X 文档中(应该是 ABMultiValueAdd())。我一定是错误地从 OP 中推断出来的……
    【解决方案2】:

    此代码有效:

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

    【讨论】:

      猜你喜欢
      • 2021-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-23
      • 2016-12-26
      相关资源
      最近更新 更多