首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何修改现有ios ABRecord地址

如何修改现有ios ABRecord地址
EN

Stack Overflow用户
提问于 2013-07-20 02:17:05
回答 1查看 398关注 0票数 2

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

代码语言:javascript
复制
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);
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-07-21 00:48:56

我刚刚意识到ABMultiValueReplaceValueAtIndex会做我想做的事。我之前的代码中有一个bug。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17752895

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档