iphone - 如何修改现有的iOS ABRecord地址

标签 iphone ios objective-c

我正在尝试编辑现有的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);

最佳答案

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

关于iphone - 如何修改现有的iOS ABRecord地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17752895/

相关文章:

c# - 不使用 MPMediaPickerController 从 iPod 库中检索所有歌曲

iphone - 如何在 iPhone 中查找 Scroll View 或 WebView 内容的高度

objective-c - 难倒了 [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier :nil] keeps returning nil

iphone - 在iPhone中使用带有userid参数的base64 haxcode将图像上传到服务器

iphone - Opengl 中的重复状态更改

iphone - 如何提高 iPhone 上 Core Data 对象插入的性能?

ios - iOS 8.4 中未调用 GIDSignInDelegate 方法

iphone - 使用谓词对核心数据进行排序以消除重复

ios - 如何检查是否启用了按钮形状?

iphone - 如何弹出一个 viewController,然后通过委托(delegate)推送一个 viewController