Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 613
Parth Bhatt Avatar asked Nov 15 '11 06:11

Parth Bhatt


People also ask

What is vCard file in iOS contacts?

You can import or export a virtual card, called a vCard. vCards contain contact information for one or more contacts.


1 Answers

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.") //

    }
}
like image 73
yvzzztrk Avatar answered Oct 02 '22 18:10

yvzzztrk