我正在尝试在“个人”记录中添加“主页”和“工作”地址。它似乎只显示1个(稍后添加的一个。如果可能的话,可以向Person添加多个地址并在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);
}
答案 0 :(得分:6)
您要做的是对两个地址使用相同的可变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);
答案 1 :(得分:1)
此代码有效:
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);