create new group with contacts framework, CNErrorDomain Code = 2

别来无恙 提交于 2019-12-21 04:29:28

问题


i try to create and save a group with the Contacts Framework. First the user authorize the App for contacts access. A viewcontroller is presented and with a + button user shows an alertview with textfield.

The user types the group name he wants and click to button of the alertview (save).

This is the code for saving the new group. The group name is available but it is not possible to save this group anyway:

CNContactStore *contactStore = [CNContactStore new];  

[contactStore requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError *error){
if (granted) {

    CNMutableGroup *newGroup = [CNMutableGroup new];
    CNSaveRequest *saveRequest = [CNSaveRequest new];

    [newGroup setName:groupName];

    //when saving to container with identifier nil, we get this error:
    //Error Domain=CNErrorDomain Code=2 "(null)" UserInfo={CNInvalidRecords=(
    //"<CNMutableGroup: 0x10a059f20: identifier=2F4981B9-8A47-45A4-8841-1FA5A09584A4:ABGroup, name=gghh>"
    [saveRequest addGroup:newGroup toContainerWithIdentifier:nil];
    [contactStore executeSaveRequest:saveRequest error:&error];

    if (error){
        //error saving group
        //NSLog(@"error message: %@",error);
    } else {
        //if no errors, reload tableview
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
    }
}
}];



Error Domain=CNErrorDomain Code=2 "(null)" UserInfo={CNInvalidRecords=(
    "<CNMutableGroup: 0x14fb3e5e0: identifier=8E490585-1223-407E-B353-0D25609B05AB:ABGroup, name=jddjd>"
)}

The next strange thing is: why is the save request trying to save this group
with identifier :ABGroup at the end?

The Error contains a info about CNInvalidRecords.
I am only using the Contacts Framework.
Why is this happening?

Any solutions for that?


回答1:


It worked fine for me, with essentially the same code.

CNMutableGroup *newGroup = [CNMutableGroup new];
CNSaveRequest *saveRequest = [CNSaveRequest new];
[newGroup setName:self.groupName];
[saveRequest addGroup:newGroup toContainerWithIdentifier:nil];
[contactStore executeSaveRequest:saveRequest error:&error];

And created a new group



来源:https://stackoverflow.com/questions/33552203/create-new-group-with-contacts-framework-cnerrordomain-code-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!