2

我正在尝试编辑现有的 iOS 联系人。联系人可以有任意数量的地址,我想编辑其中一个。我可以向联系人添加新地址,但无法对其进行编辑。也尝试过 ABMultiValueReplaceValueAtIndex 但没有帮助。这是我的代码片段

CFErrorRef error = NULL;
ABAddressBookRef addrBook = ABAddressBookCreate();
ABRecordRef existingContact = ABAddressBookGetPersonWithRecordID(addrBook, contact.deviceRecordId);

ABRecordSetValue(existingContact, kABPersonFirstNameProperty,(__bridge CFTypeRef)(contact.firstName) , &error);
ABRecordSetValue(existingContact, kABPersonLastNameProperty, (__bridge CFTypeRef)(contact.lastName) , &error);

// Get Current Addresses
ABMultiValueRef addresses = ABRecordCopyValue(existingContact, kABPersonAddressProperty);
ABMutableMultiValueRef multiAddress = ABMultiValueCreateMutableCopy(addresses);

// Append all Street line to one \n delimited string.
NSMutableString *streets = [[NSMutableString alloc] init];
if (contact.address.street1 != nil) {
    [streets appendString:contact.address.street1];
}
if (contact.address.street2 != nil) {
    [streets appendFormat:@"\n%@",contact.address.street2];
}
if (contact.address.street3 != nil) {
    [streets appendFormat:@"\n%@",[contact.address.street3 stringByReplacingOccurrencesOfString:SPAAdditionalStreetSeparator withString:@"\n"]];
}

// Single Address
NSMutableDictionary *addr = [[NSMutableDictionary alloc] init];


// Insert single address into the multi address with the specified label
CFTypeRef ctr = CFBridgingRetain(addr);
ABMultiValueAddValueAndLabel(multiAddress, ctr, (CFStringRef)@"work", NULL);
//ABMultiValueReplaceValueAtIndex(multiAddress, ctr, index);

// Add the MultiAddress value into the person object
ABRecordSetValue(existingContact, kABPersonAddressProperty, multiAddress, &error);

ABAddressBookSave(addrBook, &error);
4

1 回答 1

0

刚刚意识到 ABMultiValueReplaceValueAtIndex 会做我想要的。我之前的代码中有一个错误。

于 2013-07-20T16:48:55.620 回答