The Contacts framework provides Swift and Objective-C API to access and create a new contact. This framework is optimized for thread-safe, read-only usage.

The contact class (CNContact) has a mutable subclass CNMutableContact for use to modify contact properties like phone numbers, email addresses, an array of CNLabeledValue objects. CNLabeledValue contain label and value. Labels describe each value to the user, It allowing differentiation such as home and work for properties and you can create your own custom label using Contact Framework.

Create contact using CNMutableContact

         import Contacts

        // Contact name prefix
        contact.namePrefix = "Mr."

        // Contact first name
        contact.givenName = "Smith"

        // Contact last name
        contact.familyName = "Adam"

        // Contact profile picture
        let imageData = UIImagePNGRepresentation(#imageLiteral(resourceName: "profilePicture"))
        contact.imageData = imageData

        // Contact Address Details
        let postalAddress = CNMutablePostalAddress()
        postalAddress.street = "1 Infinite Loop"
        postalAddress.city = "Cupertino"
        postalAddress.state = "CA"
        postalAddress.postalCode = "95014"
        contact.postalAddresses = [CNLabeledValue(label:CNLabelWork, value:postalAddress)]

        // Contact Phone numbers
        contact.phoneNumbers = [CNLabeledValue(
            label:CNLabelPhoneNumberiPhone,
            value:CNPhoneNumber(stringValue: "(408) XXX-XXXX")), CNLabeledValue(label: CNLabelWork, value: CNPhoneNumber(stringValue: "(407) XXX-XXXX"))]

        // Contact Email addresses
        let homeEmailAddress = CNLabeledValue(label: CNLabelHome, value: "home@emailaddress.com" as NSString)
        let workEmailAddress = CNLabeledValue(label: CNLabelHome, value: "work@emailaddress.com" as NSString)
        contact.emailAddresses = [homeEmailAddress, workEmailAddress]

        // Contact Birthday
        var birthday = DateComponents()
        birthday.day = 2
        birthday.month = 10
        birthday.year = 1991
        contact.birthday = birthday

        // Saving the newly created contact
        let store = CNContactStore()
        let saveRequest = CNSaveRequest()
        saveRequest.add(contact, toContainerWithIdentifier:nil)
        try! store.execute(saveRequest)

Preview

enter image description here

Your comment

*

*