添加人员组到ABAddressBookRef,组添加确定,但是人没有添加到组?

时间:2010-09-23 07:46:46

标签: iphone objective-c ios

我有一些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);

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

2 个答案:

答案 0 :(得分:5)

根据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);

答案 1 :(得分:0)

我已经阅读了您必须首先将此人保存到地址簿的答案,然后创建该组,然后将该人员添加到该组,然后再次保存。

这对我不起作用。

最终对我有用的是:

将联系人保存在地址簿中,将该组保存在地址簿中,将联系人添加到该组,然后保存。非常讨厌,但很高兴它有效,并希望它适合你。

    ABAddressBookRef addressBook;
    bool wantToSaveChanges = YES;
    bool didSave;
    CFErrorRef error = NULL;

    addressBook = ABAddressBookCreateWithOptions(NULL, &error);

    // If you get some error when trying to add a contact, make sure it's not a permission issue

//    ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
//        
//    });

    // create person

    ABRecordRef aRecord = ABPersonCreate();
    CFErrorRef anError = NULL;
    bool didSet;

    didSet = ABRecordSetValue(aRecord, kABPersonFirstNameProperty, CFSTR("Lily"), &anError);
    if (!didSet) { NSLog(@"Error: %@", anError); }

    didSet = ABRecordSetValue(aRecord, kABPersonLastNameProperty, CFSTR("Bell"), &anError);
    if (!didSet) { NSLog(@"Error: %@", anError); }

    bool didAdd;

    didAdd = ABAddressBookAddRecord(addressBook, aRecord, &anError);
    if (!didAdd) { NSLog(@"Error: Couldn't add record, %@", anError); };

    // we've added the person to the address book at this point
    // now try creating a group, and adding that person to the group (failed to add, error is NULL, for whatever reason)

    // if that fails, we might need to save the added contact, then add them to the group, then save (FAILED)
    // if the above fails, save the added contact, save the group, add the contact to group, then save (PASSED)
    // if the above fails, god help us

    // saving the added person first before adding them to the group
    didSave = ABAddressBookSave(addressBook, &error);
    if (!didSave) { NSLog(@"Error: %@", error); }


    ABRecordRef group = ABGroupCreate(); // create the group
    didSet = ABRecordSetValue(group, kABGroupNameProperty, @"NYC", &anError);
    if (!didSet) { NSLog(@"Error: Couldn't set group, %@", anError); }

    didAdd = ABAddressBookAddRecord(addressBook, group, &anError);
    if (!didAdd) { NSLog(@"Error: Couldn't add group, %@", anError); }


    // save the group now
    didSave = ABAddressBookSave(addressBook, &error);
    if (!didSave) { NSLog(@"Error: %@", error); }

    // adding person to group
    didAdd = ABGroupAddMember(group, aRecord, &anError);
    if (!didAdd) { NSLog(@"Error: Couldn't add contact to group, %@", anError); }

    // final save
    didSave = ABAddressBookSave(addressBook, &error);
    if (!didSave) { NSLog(@"Error: %@", error); }

    CFRelease(addressBook);
    CFRelease(aRecord);
    CFRelease(firstName);
    CFRelease(lastName);