iPhone 上的 ABPeoplePickerNavigationController 的自定义地址簿
我正在尝试通过向设备地址簿添加新记录(基本上是将私人联系人列表与设备 AB 合并)来在 iPhone 上创建自定义地址簿。然后,我想让用户选择一个联系人,我认为我可以使用 ABPeoplePickerNavigationController
的 .addressbook
属性来完成此操作:
ABAddressBookRef contacts = ABAddressBookCreate();
for (PrivateUserType *user in rosterItems)
{
CFErrorRef err = NULL;
ABRecordRef ref = ABPersonCreate();
ABRecordSetValue(ref, kABPersonLastNameProperty, (CFStringRef)user.lastName, &err);
if (err != NULL)
{
NSString *errorStr = [(NSString *)CFErrorCopyDescription(err) autorelease];
NSLog(@"Can not set name: %@", errorStr);
}
...
ABAddressBookAddRecord(contacts, ref, &err);
if (err != NULL)
{
NSString *errorStr = [(NSString *)CFErrorCopyDescription(err) autorelease];
NSLog(@"Error adding XMPP roster user to addressbook: %@", errorStr);
}
}
ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
picker.addressBook = contacts;
[viewController presentModalViewController:picker animated:YES];
[picker release];
条目已成功添加到地址簿(我可以验证我是否在循环后执行 ABAddressBookSave()
来添加所有内容)。但是,选择器不会显示这些条目(除非我保存联系人,这当然会将所有更改保存到设备地址簿中)。
有办法做到这一点吗?这是在 iOS 4.3.1 上。
谢谢!
I am trying to create a custom addressbook on the iPhone by adding new records to the device address book (basically, merging a private contact list with the device AB). I then want to let the user select a contact, which I thought I could do with the .addressbook
property of ABPeoplePickerNavigationController
:
ABAddressBookRef contacts = ABAddressBookCreate();
for (PrivateUserType *user in rosterItems)
{
CFErrorRef err = NULL;
ABRecordRef ref = ABPersonCreate();
ABRecordSetValue(ref, kABPersonLastNameProperty, (CFStringRef)user.lastName, &err);
if (err != NULL)
{
NSString *errorStr = [(NSString *)CFErrorCopyDescription(err) autorelease];
NSLog(@"Can not set name: %@", errorStr);
}
...
ABAddressBookAddRecord(contacts, ref, &err);
if (err != NULL)
{
NSString *errorStr = [(NSString *)CFErrorCopyDescription(err) autorelease];
NSLog(@"Error adding XMPP roster user to addressbook: %@", errorStr);
}
}
ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
picker.addressBook = contacts;
[viewController presentModalViewController:picker animated:YES];
[picker release];
The entries are successfully added to the addressbook (which I can verify if I do an ABAddressBookSave()
after the loop to add everything in). However, the entries are not shown by picker
(unless I save contacts, which of course saves all changes into the device addressbook).
Is there a way to do this? This is on iOS 4.3.1.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如托罗所说,您需要首先保存对数据库的任何更改。 ABPeoplePickerNavigationController 不适用于未提交的数据或自定义数据。
你可以在苹果开发论坛上看到相当新鲜的话题
https://devforums.apple.com/message/370070#370070
在那里提出了类似的问题。苹果公司的人说:“我看不出有什么办法可以做到这一点。”但他建议为此修复一个错误,以便在未来的 iOS 版本中实现此功能。
您现在所能做的就是从头开始为这个 AddressBook 编写您自己的 UI :(
更新:呵呵。我也在应用程序开发论坛上找到了您的问题)))
As Toro said, you need save any changes to database first. ABPeoplePickerNavigationController not intended to work with uncommit data or with custom data.
You can see rather fresh topic on Apple Dev Forums
https://devforums.apple.com/message/370070#370070
where similar question was asked. And guy from Apple says that "I can't see any way to do this." But he suggest filling a bug for this to implement this feature in future release of iOS.
All you can do now is code your own UI for this AddressBook from the ground :(
updated: he-he. I found your question on App Dev Forum too )))
您需要通过 ABAddressBookSave() 方法保存对数据库的任何更改。
苹果文档链接。
You need to save any changes to database by ABAddressBookSave() method.
The apple doc link.