1

我正在尝试将选定的联系人添加到特定组中,但联系人无法添加到组中。我想创建新组并将联系人添加到该组中。但是在该组中创建组后无法添加联系人。这是我创建新组并将联系人添加到组中的代码:

    addressBook = ABAddressBookCreateWithOptions(nil, &err);
    group = ABGroupCreate();
    ABRecordSetValue(group, kABGroupNameProperty, txtGroupNameText.text, &err);
    ABAddressBookAddRecord(addressBook, group, &err);
    ABAddressBookSave(addressBook, &err);
    groupId = ABRecordGetRecordID(group);

并添加联系人:

        ABRecordRef person = [contactArray objectAtIndex:i];
        ABRecordRef HiByeGroup = ABAddressBookGetGroupWithRecordID(addressBook,       self.groupId);
        BOOL didAdd = ABGroupAddMember(HiByeGroup,person,&err);

        if (didAdd) {
            // Update to handle the error appropriately.
            NSLog(@"Unresolved error while adding person to HiBye group");
        }

        BOOL didSave = ABAddressBookSave(addressBook, &err);

        if (didSave) {
            // Update to handle the error appropriately.
            NSLog(@"Unresolved error while saving address book");
        }

联系人保存但未添加。请帮助我,谢谢。

4

1 回答 1

3

您首先需要先将 Person 保存到通讯簿,然后再将其添加到组中。

例如试试这个代码,它运行良好

ABAddressBookRef ab = ABAddressBookCreate();
CFErrorRef error;
ABRecordRef group = ABGroupCreate();
ABRecordSetValue(group, kABGroupNameProperty,@"new group", &error);
ABAddressBookAddRecord(ab, group, &error);
ABAddressBookSave(ab, &error);
//Create new person and save to this group
ABRecordRef record = ABPersonCreate();
BOOL isSuccess ;

isSuccess  = ABRecordSetValue(record, kABPersonNicknameProperty,@"GroupMember nick name", &error);
isSuccess = ABRecordSetValue(record, kABPersonMiddleNameProperty, @"Middle name", &error);

ABMutableMultiValueRef copyOfPhones = ABMultiValueCreateMutable(kABPersonPhoneProperty);

CFTypeRef phone= CFSTR("123000222111");

ABMultiValueAddValueAndLabel(copyOfPhones, phone,kABPersonPhoneMobileLabel,NULL);

isSuccess = ABRecordSetValue(record, kABPersonPhoneProperty, copyOfPhones, &error);

isSuccess = ABAddressBookAddRecord(ab, record, &error);
isSuccess = ABAddressBookSave(ab, &error);

ABGroupAddMember(group, record, &error);

NSLog(@"is success %d", isSuccess);

ABAddressBookSave(ab, &error);
CFRelease(group); 
于 2013-03-18T10:11:41.730 回答