create new group with contacts framework, CNErrorDomain Code = 2
Asked Answered
V

1

7

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?

Vow answered 5/11, 2015 at 18:33 Comment(0)
M
3

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

Mitre answered 12/11, 2015 at 14:40 Comment(8)
this creates a new group if, provided iCloud is set as default in Settings > Mail, Calendar, Contacts. If the default is an Exchange, then it will not be possible to create and save groups. And thats why i get this error. I am searching for a workaround for this problem.Vow
According to Apple's docs you can't create a group in an Exchange container, you need to create a separate container for each "group".Mitre
yes thats right, but i guess it would not be possible to create another seperate container to create a new group and save it locally WHEN iCloud sync is activated.Vow
There isn't a way to find what the current default address book type is, is there? (I don't think there used to be in the old API) perhaps something that doesn't cause an exception, but only works on one type, or the other?Mitre
i didnt find out a way how to get the current default addess book type. The Contacts Framework throws an error(see my question post) and the old AB Framework does not provide any errors.Vow
Can you not check CNContainerType on the current container, to see if it equals CNContainerTypeExchange?Mitre
i didnt try that but i assume, yes it is possible to check this. but the real question is, what is the benefit? what can i do when i know it is exchange..?Vow
We could at least warn the user instead of crashing :) as Exchange uses containers effectively as groups, I thought we might be able to create another container and use that as we would a group, but I have not looked into this yet.Mitre

© 2022 - 2024 — McMap. All rights reserved.