Link to home
Start Free TrialLog in
Avatar of Ross Hambleton
Ross Hambleton

asked on

Saving contact using CNGroup Identifier

Hi All

I'm currently developing an iOS app using the Contacts Framework to add contacts into a specific Group.

The code currently checks to see if the group exists, if it doesn't, it creates it and stores the Identifier into the UserDefaults store, if not it reads the identifier ready to save the contact later on.

let defaults = UserDefaults.standard
        var groupID = defaults.string(forKey: "contactsGroupID")
        let group = CNMutableGroup()
        var newGroupNeeded = Bool()
        
        if groupID?.isEmpty ?? true {
            let groupName = "my App Contact Group"
            let groupSave = CNSaveRequest()
            let newGroupNeeded = true
            group.name = groupName
            groupSave.add(group, toContainerWithIdentifier: nil)

            do {
                try store.execute(groupSave)
               defaults.set(group.identifier, forKey: "contactsGroupID")
               defaults.set(groupName, forKey: "contactsGroupName")
                print("New Identifer saved: \(group.identifier)")
            } catch let error{
                print(error)
                print()
            }
        }
        else {
            let newGroupNeeded = false
            
            let groupName = defaults.object(forKey: "contactsGroupName")
            var predicate: NSPredicate? = nil
            if let object = [
                defaults.object(forKey: "contactsGroupID")
                ] as? [String] {
                predicate = CNGroup.predicateForGroups(withIdentifiers: object)
            }
            var foundGroups = try? store.groups(matching: predicate)

                let group = foundGroups?.first! as? CNGroup
            print("FOUND GROUP: \(group?.name) & \(group?.identifier)")
            }

Open in new window


This code above all seems to work fine, output is "FOUND GROUP: Optional("my App Contact Group") & Optional("B03C7C3C-5BC7-4ED8-9711-E72E3091EE4E:ABGroup")"

The weird thing is that the following code will save the contact to the group correctly when it is creating the group for the first time, but when the group already exists it errors.

//Code above here sets the MutableContact, name, tele number etc etc

let request2 = CNSaveRequest()

        request2.addMember(newContact, to: group)
        request2.add(newContact, toContainerWithIdentifier: nil)
    
        do{
            try store.execute(request2)
        } catch let error{
            print(error)
        }

Open in new window


As i said before, it works fine before when creating a new contact but i get the following error when trying to add a contact to an existing group.

Error Domain=CNErrorDomain Code=200 "Updated Record Does Not Exist" UserInfo={CNInvalidRecords=(
    "<CNMutableGroup: 0x600000d65a80: identifier=EBAF677C-7076-4B55-AC84-84BE705390FF:ABGroup, name=(null)>"
), 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}

There doesnt seem to be much online to do with Swift and the Contacts Framework so hoping someone in here might be able to advise?

Thanks All
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.