为什么ABGroupAddMember失败?它返回NO并且不设置CFErrorRef值

时间:2012-11-13 15:55:27

标签: ios contacts abaddressbook

我需要阻止在我们的应用程序中显示联系人组,但是用户只需从AddressBook中删除组即可。所以我试图在显示联系人之前删除它们,然后在完成后将它们添加回来,以便AddressBook保持不变,iOS Contacts应用程序按原样显示组。我创建了两个用于存储信息的数组:

NSArray *aGroups;
NSMutableArray *aGroupMembers;

我删除组,存储它们,并在viewWillAppear中显示选择器:

// Remove group records for the life of this view.
CFErrorRef error;
ABAddressBookRef abRef = ABAddressBookCreate();
NSArray *groups = (NSArray *)ABAddressBookCopyArrayOfAllGroups(abRef);
if (groups.count > 0) {

    // we will remove the groups so save for restoration
    self.aGroups = [[NSArray alloc] initWithArray:groups];
    aGroupMembers = [[NSMutableArray alloc] initWithCapacity:groups.count];
    for (int i = 0; i < groups.count; i++) {

        NSArray *members = (NSArray *)ABGroupCopyArrayOfAllMembers([groups objectAtIndex:i]);
        NSMutableArray *memberIDs = [[NSMutableArray alloc] initWithCapacity:[members count]];
        for (id member in members) {
            ABRecordID ID = ABRecordGetRecordID(member);
            [memberIDs addObject:[NSNumber numberWithInteger:ID]];
        }

        [aGroupMembers insertObject:memberIDs atIndex:i];
        CFRelease(members);
        [memberIDs release];

        // Remove the group from the addressbook
        ABAddressBookRemoveRecord(abRef, [groups objectAtIndex:i], &error);
    }

    CFRelease(groups);
    ABAddressBookSave(abRef, nil);
    self.picker.addressBook = abRef;
}

CFRelease(abRef);

然后在viewWillDisappear和willResignActive中,我“尝试”重新创建组,将成员重新添加到每个组并添加回地址簿。但是由于ABGroupAddMember()失败,我无法再添加成员。当我检查调试器中的变量时,GroupName和person的firstname都是正确的。我看不到问题,CFErrorRef值没有设置。

// Restore the group records.
if (self.aGroups != nil) {

    CFErrorRef error = nil;
    CFTypeRef grpName = nil;
    CFTypeRef firstName = nil;
    ABRecordRef newGroup = nil;
    ABAddressBookRef abRef = ABAddressBookCreate();

    // Re-create the groups
    for (int i = 0; i < self.aGroups.count; i++) {

        // Create the new group
        newGroup = ABGroupCreate();
        grpName = ABRecordCopyValue((ABRecordRef)[self.aGroups objectAtIndex:i], kABGroupNameProperty);
        ABRecordSetValue(newGroup, kABGroupNameProperty, grpName, &error);

        // Create the members
        NSArray *memberIDs = (NSArray*)[aGroupMembers objectAtIndex:i];
        for (NSNumber *iD in memberIDs) {

            ABRecordRef person = ABAddressBookGetPersonWithRecordID(abRef,iD.intValue);
            firstName = ABRecordCopyValue((ABRecordRef)person, kABPersonFirstNameProperty);                
            BOOL bSuccess = ABGroupAddMember(newGroup, person, &error);
            if (!bSuccess) {
                //NSString *errorStr = [(NSString *)CFErrorCopyDescription(error) autorelease]; // this cause EXEC_BAD_ACCESS
                CFRelease(error);
            }
        }

        CFRelease(newGroup);
        CFRelease(grpName);
    }

    // Save the changes
    ABAddressBookSave(abRef, nil);

    CFRelease(abRef);
    self.aGroups = nil;
    [aGroupMembers removeAllObjects];
    aGroupMembers = nil;
}

2 个答案:

答案 0 :(得分:2)

尝试将通过iPhone协议从Google CardDAV同步的联系人添加到以编程方式创建的本地群组时发生这种情况。

事实证明,您无法将同步的联系人添加到本地组,反之亦然,但它只返回NO而不将error参数设置为任何错误消息,以解释它为什么没有工作。

我在我的应用中通过尝试将联系人添加到群组中解决了它,如果它不起作用,我通过在我的应用中本地保存联系人群组关系来解决它。

不是那么好,如果有人有办法解决这个问题,我很乐意听到。

答案 1 :(得分:1)

在尝试使用ABAddressBookAddRecord向群组添加成员之前,您可能需要使用ABGroupAddMember将群组记录添加到地址簿中。