我正尝试在个人记录中添加“家庭”和“工作”地址。似乎只有 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/