How to save contact info from vCard into iPhone's Contacts App

Parth Bhatt picture Parth Bhatt · Nov 15, 2011 · Viewed 10.2k times · Source

In my iPhone app, I want to save the vCards into my iPhone's Contacts when I click onto the vCard which I have.

How can I do that?

I have seen an app on app store which does this:

http://itunes.apple.com/us/app/read-vcard/id402216831?mt=8

Thanks

Answer

yvzzztrk picture yvzzztrk · Oct 27, 2015

New Contacts Framework introduced with iOS9, saving vCard data into iPhone's contacts is much easier and simpler with Swift4.

import Contacts

    func saveVCardContacts (vCard : Data) { // assuming you have alreade permission to acces contacts

    if #available(iOS 9.0, *) {

        let contactStore = CNContactStore()

        do {

            let saveRequest = CNSaveRequest() // create saveRequests

            let contacts = try CNContactVCardSerialization.contacts(with: vCard) // get contacts array from vCard

            for person in contacts{

                saveRequest.add(person as! CNMutableContact, toContainerWithIdentifier: nil) // add contacts to saveRequest

            }

            try contactStore.execute(saveRequest) // save to contacts

        } catch  {

            print("Unable to show the new contact") // something went wrong

        }

    }else{

        print("CNContact not supported.") //

    }
}