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?

brush51
  • 5,691
  • 6
  • 39
  • 73

1 Answers1

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

Peter Johnson
  • 3,764
  • 1
  • 23
  • 27
  • 1
    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. – brush51 Nov 12 '15 at 21:11
  • 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". – Peter Johnson Nov 12 '15 at 21:48
  • 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. – brush51 Nov 12 '15 at 21:50
  • 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? – Peter Johnson Nov 12 '15 at 22:03
  • 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. – brush51 Nov 13 '15 at 12:34
  • Can you not check CNContainerType on the current container, to see if it equals CNContainerTypeExchange? – Peter Johnson Nov 13 '15 at 14:50
  • 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..? – brush51 Nov 13 '15 at 22:05
  • 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. – Peter Johnson Nov 13 '15 at 23:57