ioscncontactstore

add a contact to a group using Contacts framework


I created a group using contacts group, and then I want to add a contact to the group.

        NSPredicate *predicate = [CNGroup predicateForGroupsWithIdentifiers:@[[[NSUserDefaults standardUserDefaults]objectForKey:@"groupIDentifier"]]];
        NSArray *groups = [store groupsMatchingPredicate:predicate error:&saveError];

        CNMutableContact *contact = [[CNMutableContact alloc] init];
        contact.familyName = @"Doe";
        contact.givenName = @"John";

        CNLabeledValue *homePhone = [CNLabeledValue labeledValueWithLabel:CNLabelHome value:[CNPhoneNumber phoneNumberWithStringValue:@"312-555-1212"]];
        contact.phoneNumbers = @[homePhone];

        CNSaveRequest *request = [CNSaveRequest new];
        CNGroup *group = [groups firstObject];
        [request addContact:contact toContainerWithIdentifier:group.identifier];

        if (![store executeSaveRequest:request error:&saveError]) {
            NSLog(@"error = %@", saveError);
        }

The error is:

error = Error Domain=CNErrorDomain Code=200 "Updated Record Does Not Exist" UserInfo={CNInvalidRecordIdentifiers=( "45FFBB0D-C74B-4A14-8293-9099EA7DEF81:ABGroup" ), NSLocalizedDescription=Updated Record Does Not Exist, NSLocalizedFailureReason=The save request failed because it updates a record that does not exist or has already been deleted.}

I also tried using:

[request addMember:contact toGroup:[groups firstObject]];

and this case, the error is:

error = Error Domain=CNErrorDomain Code=200 "Updated Record Does Not Exist" UserInfo={CNInvalidRecords=(
    "<CNContact: 0x7f8ce97aa640: identifier=7CC6BC1D-1B23-48DA-8282-06115F542A97:ABPerson, givenName=John, familyName=Doe, organizationName=, phoneNumbers=(\n    \"<CNLabeledValue: 0x600001873cc0: identifier=68277209-3AE4-40AF-9EEA-DF0E1D01883C, label=_$!<Home>!$_, value=<CNPhoneNumber: 0x600000433300: stringValue=312-555-1212, initialCountryCode=(null)>>\"\n), emailAddresses=(\n), postalAddresses=(\n)>" ), NSLocalizedFailureReason=The save request failed because it updates a record that does not exist or has already been deleted., NSLocalizedDescription=Updated Record Does Not Exist}

Solution

  • The crazy thing I found is: I need to call both addMember and addContact, to actually make the contact add to the group.

        CNGroup *group = [groups firstObject];
        [request addMember:contact toGroup:group];
        [request addContact:contact toContainerWithIdentifier:nil];
    
    if (![store executeSaveRequest:request error:&saveError]) {
            NSLog(@"error = %@", saveError);
        }
    

    Response from the Apple Support:

    You must save the contact, and group, separately. Then check to see if they exist, before the contact can be added to that group. Otherwise, you will receive the same error you are seeing in the output.

    1. Create the group
    2. Execute the save of the contact
    3. Check if the Contact and the Group both exist
    4. Add contact to group using addMember

    This actually serves the purpose, but I have no clue why I have to actually make the two types of requests.