问题
I am trying to add selected contacts into specific group but contacts cant added in group. I want to create new group and add contacts into that group. but after creating group in that group unable to add contacts.here is my code for create new group and add contacts into group:
addressBook = ABAddressBookCreateWithOptions(nil, &err);
group = ABGroupCreate();
ABRecordSetValue(group, kABGroupNameProperty, txtGroupNameText.text, &err);
ABAddressBookAddRecord(addressBook, group, &err);
ABAddressBookSave(addressBook, &err);
groupId = ABRecordGetRecordID(group);
and for adding contacts :
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");
}
contacts get save but not getting add.Please help me ,Thanks.
回答1:
You first need to save the Person to the address book before adding it to the group.
for example try this code , it works well
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);
来源:https://stackoverflow.com/questions/15470733/unable-to-add-contacts-into-specific-group-in-iphone-using-abgroupaddmember