在iPhone中以编程方式将联系人添加到现有组 [英] Add Contact to Existing Group programatically in iphone

查看:93
本文介绍了在iPhone中以编程方式将联系人添加到现有组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将ABRecordRef添加到现有组。一些如何不起作用。这是我的代码:

 -(void)addUsers:(NSMutableArray *)users到GroupID:(ABRecordID)groupID {

CFErrorRef错误= NULL;
ABAddressBookRef addressBookRef = ABAddressBookCreate();

//获取组
ABRecordRef group = ABAddressBookGetGroupWithRecordID(addressBookRef,groupID);

for(用户*用户中的用户){

int recordId = user.uniqID;

ABRecordRef person = ABAddressBookGetPersonWithRecordID(addressBookRef,(ABRecordID)recordId);

//将新人员添加到记录中
ABAddressBookAddRecord(addressBookRef,person,nil);
ABAddressBookSave(addressBookRef,& error);

//添加组
ABAddressBookAddRecord(addressBookRef,group,& error);
ABAddressBookSave(addressBookRef,& error);

//将人员添加到组
ABGroupAddMember(group,person,& error);
ABAddressBookSave(addressBookRef,& error);

//保存记录
ABAddressBookSave(addressBookRef,nil);

if(!isMemberAdded){
[self showAlertWithTitle:@ Alert andMessage:@将用户添加到现有组时发生某些错误];
}
}
BOOLisMember⚓ToGroup= ABAddressBookSave(addressBookRef,nil);
if(isMember☎联系人){
[self showAlertWithTitle:@ Alert andMessage:@已成功将所选成员添加到现有组];
}
else {
[self showAlertWithTitle:@ Alert andMessage:@将用户添加到现有组时发生某些错误];
}
CFRelease(addressBookRef);
}

每当我使用上述代码&尝试将其保存在现有组中,我在控制台中得到以下消息

  CPSqliteStatementPerform:INSERT INTO ABGroupMembers的约束失败(group_id,member_type, member_id)VALUES(?,?,?); 
CPSqliteStatementReset:列group_id,member_type,member_id对于INSERT INTO ABGroupMembers(group_id,member_type,member_id)值(?,?,?)而言不是唯一的;

我没有得到上面代码中缺少的内容。
任何帮助都将受到赞赏。谢谢

解决方案

如果看到以下消息:

  CPSqliteStatementPerform:INSERT INTO ABGroupMembers(group_id,member_type,member_id)VALUES(?,?,?)的约束失败。 
CPSqliteStatementReset:列group_id,member_type,member_id对于INSERT INTO ABGroupMembers(group_id,member_type,member_id)值(?,?,?)而言不是唯一的;

也在文档


ABAddressBook的不透明类型(其实例称为地址
的书)提供了地址簿的编程接口-一个
集中式数据库,供多个应用程序用来存储有关人的个人
信息


(因此,在内部,此错误是由于不正确的sqlite查询引起的。)



它表示 ABGroupAddMember ABAddressBookAddRecord(addressBookRef,group,& error); 调用存在一些问题。 / p>

问题可能是:当我们调用 ABAddressBookGetGroupWithRecordID 和<$ c $时,我们已经有了组和个人引用。 c> ABAddressBookGetGroupWithRecordID 函数。



现在,当对(个人组都执行 ABAddressBookAddRecord 时code>)会将其添加到通讯录,但不更新其UniqueIds ((个人和组引用的ID 。),因此在 ABGroupAddMember 函数调用时,它具有 person group 具有与 ABGroupMembers 表中添加的ID相同的ID。



请,请尝试为 group 打印 recordId 将它们添加到地址簿(以及在将它们添加到组之前)。如果 recordsIds 未更新,则是原因,您将需要获取 record 来获取新的 recordId 。还要检查是否将新的 person group 添加到地址簿。



*当前,我无法访问我的建议/答案。


I want to add ABRecordRef to existing Group. Some how its not working. Here is my code:

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

Whenever I use above code & try to save it in existing group I get following message in console

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 (?, ?, ?);

I am not getting what I am missing in above code. Any knid of help is appreciated. Thanks

解决方案

If we see the message:

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 (?, ?, ?);

Also in documentation :

The ABAddressBook opaque type (whose instances are known as address books) provides a programming interface to the Address Book—a centralized database used by multiple applications to store personal information about people

(So, internally, this error is caused due to incorrect sqlite query.)

It indicates that there is some issue with ABGroupAddMember or ABAddressBookAddRecord(addressBookRef, group, &error); call.

The problem can be: We have group and person references already with us when we callABAddressBookGetGroupWithRecordID and ABAddressBookGetGroupWithRecordID functions.

Now, when ABAddressBookAddRecord is executed for both( person and group) it is adding them to addressbook but not updating their UniqueIds(id for person and group ref.) so at the time of ABGroupAddMember function call it has person and group with same ids which are added in ABGroupMembers table.

Please, try to print recordId for person and group after you add them to address book(and before you add them to group). if recordsIds are not updated then that is the cause, and you will need to fetch record for new recordId. Also check whether the new person and group are being added to addresbook or not.

*Currently, I do not mac access to verify my suggestion/answer.

这篇关于在iPhone中以编程方式将联系人添加到现有组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆