OStack程序员社区-中国程序员成长平台

标题: iphone - 将人员组添加到 ABAddressBookRef,组添加正常,但人员未添加到组? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 16:19
标题: iphone - 将人员组添加到 ABAddressBookRef,组添加正常,但人员未添加到组?

我有几行 Objective-C 代码,例如:

ABAddressBookRef addressBook;
CFErrorRef error = NULL;
addressBook = ABAddressBookCreate();
ABRecordRef group = ABGroupCreate();
ABRecordSetValue(group, kABGroupNameProperty,@"My Group", &error);

ABRecordRef person = ABPersonCreate();
ABRecordSetValue(person,kABPersonFirstNameProperty,@"Huy 1111111",&error);
ABRecordSetValue(person,kABPersonLastNameProperty,@"AseDra",&error);
ABGroupAddMember(group, person, &error);

ABAddressBookAddRecord(addressBook, group, &error);

ABAddressBookSave(addressBook,&error);

它运行正常,没有错误,组已添加到模拟器通讯簿但人没有添加到模拟器通讯簿。谁能告诉我哪里错了?



Best Answer-推荐答案


根据 iOS 的通讯簿编程指南(下面的链接),通讯簿中必须存在人员记录,然后才能将其添加到组中。

“在可以将人员记录添加到组之前,它必须已经保存到通讯簿数据库。”

http://developer.apple.com/library/ios/#documentation/ContactData/Conceptual/AddressBookProgrammingGuideforiPhone/500-DirectInteraction/DirectInteraction.html%23//apple_ref/doc/uid/TP40007744-CH6-SW4

所以也许这会起作用

ABAddressBookRef addressBook;
CFErrorRef error = NULL;
addressBook = ABAddressBookCreate();

ABRecordRef person = ABPersonCreate();
ABRecordSetValue(person,kABPersonFirstNameProperty,@"Huy 1111111",&error);
ABRecordSetValue(person,kABPersonLastNameProperty,@"AseDra",&error);

// add the person record
ABAddressBookAddRecord(addressBook, person, &error);

ABRecordRef group = ABGroupCreate();
ABRecordSetValue(group, kABGroupNameProperty,@"My Group", &error);
ABGroupAddMember(group, person, &error);
ABAddressBookAddRecord(addressBook, group, &error);

ABAddressBookSave(addressBook,&error);

关于iphone - 将人员组添加到 ABAddressBookRef,组添加正常,但人员未添加到组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3776299/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4