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 (?, ?, ?);
上記のコードで欠落しているものが得られません。どんな助けでも大歓迎です。ありがとう