无法使用ABGroupAddMember将联系人添加到iphone中的特定组中

时间:2013-03-18 06:13:32

标签: iphone objective-c addressbook

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

    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");
        }

联系人获取保存但没有获得添加。请帮助我,谢谢。

2 个答案:

答案 0 :(得分:3)

首先需要将人员保存到地址簿,然后再将其添加到群组中。

例如试试这段代码,效果很好

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

答案 1 :(得分:1)

ABAddressBookRef _addressBook;
CFErrorRef error;
_addressBook = ABAddressBookCreateWithOptions(NULL, &error);

ABRecordRef _source;
CFArrayRef sourceArray = ABAddressBookCopyArrayOfAllSources(_addressBook);
if (sourceArray != NULL) {
    CFIndex count = CFArrayGetCount(sourceArray);
    CFIndex i=0;
    BOOL foundLocalSource = FALSE;
    while ( (i<count) && (!foundLocalSource) ) {
        ABRecordRef record = CFArrayGetValueAtIndex(sourceArray, i++);

        // determine source type
        CFNumberRef sourceTypeNum = (CFNumberRef)ABRecordCopyValue(record, kABSourceTypeProperty);
        ABSourceType sourceType;
        CFNumberType numType = CFNumberGetType(sourceTypeNum);
        CFNumberGetValue(sourceTypeNum, numType, &sourceType);
        CFRelease(sourceTypeNum);
        ABSourceType cleanSsourceType = (sourceType & (~kABSourceTypeSearchableMask));
        if (cleanSsourceType == kABSourceTypeLocal) {
            foundLocalSource = true;
            ABRecordID recordId = ABRecordGetRecordID(record);
            _source = ABAddressBookGetSourceWithRecordID(_addressBook, recordId);
        }
    }
    CFRelease(sourceArray);
}

// Create Group
ABRecordRef _group;
_group = ABGroupCreateInSource(_source);
ABRecordSetValue (_group, kABGroupNameProperty, CFSTR("Yong's Group"), &error);
//save group record
if (ABAddressBookAddRecord (_addressBook, _group, &error)) {
    NSLog(@"Accounts group successfully added to addressbook!");
}

if (ABAddressBookSave(_addressBook, &error)) {
    NSLog(@"Address Book Saved successfully!");
}
// create Person In Group
ABRecordRef result = ABPersonCreateInSource(_source);
ABRecordSetValue(result, kABPersonLastNameProperty,(CFTypeRef)@"yong03", &error);

CFErrorRef errorRef;

if (ABGroupAddMember(_group, result, &errorRef)) {
    NSLog(@"ABGroupAddMember successfully!");
}

if (ABAddressBookAddRecord(_addressBook, result, &errorRef)) {
    NSLog(@"ABAddressBookAddRecord successfully!");
}

if (ABAddressBookSave(_addressBook, &errorRef)) {
    NSLog(@"ABAddressBookSave successfully!");
}