Create contact in a group

170 Views Asked by At

I want to create a functionality, wherein some contacts will be saved in user's phone. Now I know using Contact framework, I can save the contacts. This is what I have been using.

func saveContacts(dict: [[String : String]]) {
        for contactDict in dict {
            let fname = contactDict["name"]
            let lname = contactDict["last_name"]
            let phone = contactDict["phone"]
            
            print(fname, lname, phone)
            
            let contact = CNMutableContact()
            
           
            
            contact.givenName = fname!
            contact.familyName = lname!
            
            
            contact.phoneNumbers = [CNLabeledValue(
                label:CNLabelPhoneNumberiPhone,
                value:CNPhoneNumber(stringValue:phone!))]
            
            
            
            let saveRequest = CNSaveRequest()
            saveRequest.add(contact, toContainerWithIdentifier:nil)
            do{
                try store.execute(saveRequest)
            }
            catch{
                print("error is \(error)")
            }
            
        }
    }

But what I exactly want is to create a group with my app name, let's say XYZ and my code should check that if that group XYZ is already there new contact should be saved in there otherwise create the contact group and save contacts there.

Thanks in advance!

0

There are 0 best solutions below