我想将 ABRecordRef 添加到现有组。有些如何它不起作用。这是我的代码:
-(void)addUsers:(NSMutableArray*)users toGroupID:(ABRecordID)groupID {
CFErrorRef error = NULL;
ABAddressBookRef addressBookRef = ABAddressBookCreate();
// Get Group
ABRecordRef group = ABAddressBookGetGroupWithRecordID(addressBookRef,groupID);
for (User *user in users) {
int recordId = user.uniqID;
ABRecordRef person = ABAddressBookGetPersonWithRecordID(addressBookRef,(ABRecordID)recordId);
//add the new person to the record
ABAddressBookAddRecord(addressBookRef, person, nil);
ABAddressBookSave(addressBookRef, &error);
// add the group
ABAddressBookAddRecord(addressBookRef, group, &error);
ABAddressBookSave(addressBookRef, &error);
// add the person to the group
ABGroupAddMember(group, person, &error);
ABAddressBookSave(addressBookRef, &error);
//save the record
ABAddressBookSave(addressBookRef, nil);
if (!isMemberAdded) {
[self showAlertWithTitle:@"Alert" andMessage:@"Some Error Occured While Adding User to Existing Group"];
}
}
BOOL isMembersAddedToGroup = ABAddressBookSave(addressBookRef, nil);
if (isMembersAddedToGroup) {
[self showAlertWithTitle:@"Alert" andMessage:@"Selected Members Added to Existing Group Successfully"];
}
else{
[self showAlertWithTitle:@"Alert" andMessage:@"Some Error Occured While Adding User to Existing Group"];
}
CFRelease(addressBookRef);
}
每当我使用上述代码并尝试将其保存在现有组中时,我都会在控制台中收到以下消息
CPSqliteStatementPerform: constraint failed for INSERT INTO ABGroupMembers (group_id, member_type, member_id) VALUES (?, ?, ?);
CPSqliteStatementReset: columns group_id, member_type, member_id are not unique for INSERT INTO ABGroupMembers (group_id, member_type, member_id) VALUES (?, ?, ?);
我没有得到上面代码中缺少的东西。感谢任何帮助。谢谢