6

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

Parth Bhatt
  • 19,381
  • 28
  • 133
  • 216
  • I wonder if its possible just to send it to a Gmail account and import it that way? Oh you'll probably get a better answer from the guys over at http://apple.stackexchange.com/ – Yzmir Ramirez Nov 15 '11 at 06:32
  • @Yzmir Ramirez: This is a question from programming point of view not the iPhone (hardware) functionality point of view. So I think this is proper question of stackoverflow. I want to do this programmatically through my application. – Parth Bhatt Nov 15 '11 at 06:36
  • My bad. I wish I could help. The vCard format isn't that tough to parse, but I too would like to know if there's something built-in to solve this programatically. – Yzmir Ramirez Nov 15 '11 at 06:57
  • @ParthBhatt: I dont know about vCard, but i can help you to insert information in iPhone's contacts. If you need let me know. – Devang Nov 15 '11 at 07:00
  • Thankfully, iOS 7 *finally* allows direct importing of vCards into Contacts. – Blazemonger Oct 03 '13 at 20:45
  • @Have you written code to handle if there are duplicates of the vCard in the device already? – daniel Jun 01 '23 at 12:29

5 Answers5

5

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

    }
}
Balakrishnan Mca
  • 747
  • 5
  • 11
yvzzztrk
  • 424
  • 6
  • 13
  • 2
    Thank you for the value addition to this post..!! – Parth Bhatt Oct 29 '15 at 13:53
  • 1
    @yvzzztrk, only for 9.0? i am new to ios app development, but i am developing from ios 8.0. What is 8.0 equivalent for CNContact? – alex Nov 03 '15 at 13:19
  • 1
    hi @alex, you can use **ABAddressBoook** , look at this [answer](http://stackoverflow.com/questions/8837828/unable-to-create-a-person-using-vcard-representation/33274235#33274235) – yvzzztrk Nov 04 '15 at 12:37
  • @yvzzztrk i follow your link and got it to work with your answer. Another question do you have a link how to first show a popup with vcard fields before saving? – alex Nov 04 '15 at 16:05
  • 1
    @alex, look at [AddressBookUI](https://developer.apple.com/library/prerelease/ios/documentation/AddressBookUI/Reference/AddressBookUI_Framework/index.html) framework, and [ABUnknownPersonViewController](https://developer.apple.com/library/prerelease/ios/documentation/AddressBookUI/Reference/ABUnknownPersonViewController_Class/index.html#//apple_ref/occ/cl/ABUnknownPersonViewController) – yvzzztrk Nov 05 '15 at 05:39
  • @yvzzztrk thank you! last question when i create it for ios 8 will it work in ios 9 as well? – alex Nov 05 '15 at 09:18
  • 1
    @alex, yes of course,but I suggest you to use **CNContact** framework for iOS 9 with **if #available(iOS 9.0, *) { /*code*/ }** as in the my answer – yvzzztrk Nov 05 '15 at 09:54
  • can you do this also in objective-c using CNContact – Abhishek Thapliyal Apr 07 '16 at 14:45
  • `saveRequest.add(person as! CNMutableContact, toContainerWithIdentifier: nil)` crashes for me. It says that it "could not cast value of type 'CNContact' (0x1142c85f8) to 'CNMutableContact' (0x1142c7568)". Swift 3, iOS 9. – Fomentia Nov 11 '16 at 18:51
3

Following is the code for adding user information in iPhone's Contact.

As I told you that i dont know anything about the vCard, but this code posted by malinois in their answer here might be of use:

ABAddressBookRef addressBook = ABAddressBookCreate(); // create address book record 
ABRecordRef person = ABPersonCreate(); // create a person  

NSString *phone = @"0123456789"; // the phone number to add  

//Phone number is a list of phone number, so create a multivalue  
ABMutableMultiValueRef phoneNumberMultiValue = ABMultiValueCreateMutable(kABPersonPhoneProperty); 
ABMultiValueAddValueAndLabel(phoneNumberMultiValue ,phone,kABPersonPhoneMobileLabel, NULL);

ABRecordSetValue(person, kABPersonFirstNameProperty, @"FirstName" , nil); // first name of the new person 
ABRecordSetValue(person, kABPersonLastNameProperty, @"LastName", nil); // his last name 
ABRecordSetValue(person, kABPersonPhoneProperty, phoneNumberMultiValue, &anError); // set the phone number property 
ABAddressBookAddRecord(addressBook, person, nil); //add the new person to the record

ABRecordRef group = ABGroupCreate(); //create a group 
ABRecordSetValue(group, kABGroupNameProperty,@"My Group", &error); // set group's name 
ABGroupAddMember(group, person, &error); // add the person to the group         
ABAddressBookAddRecord(addressBook, group, &error); // add the group   

ABAddressBookSave(addressBook, nil); //save the record  

CFRelease(person); // relase the ABRecordRef  variable 
Community
  • 1
  • 1
Devang
  • 11,258
  • 13
  • 62
  • 100
1

func saveContactsfromVCard(vCard : Data)
    {            
            let contactStore = CNContactStore()

            do {

                let saveRequest = CNSaveRequest() // create saveRequests

                let fetchedContacts = try CNContactVCardSerialization.contacts(with: vCard)

                for person in fetchedContacts{

                   let mutableContact = person.mutableCopy() as! CNMutableContact

                    saveRequest.add(mutableContact, toContainerWithIdentifier: nil)    
                   // saveRequest.add(person, toContainerWithIdentifier: nil) // add contacts to saveRequest

                }

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

                let alert1 = UIAlertController(title: "Successful", message: "Contacts Added Successfully!", preferredStyle: UIAlertController.Style.alert)

                alert1.addAction(UIAlertAction(title: "Ok", style: UIAlertAction.Style.default, handler:
                    {(alertAction) in
                }))
                self.present(alert1, animated: true, completion: nil)


            } catch  {

                print("Unable to show the new contact")
        }else{

            print("Contacts not added")

        }
    }
Nikhil Viradiya
  • 87
  • 2
  • 15
1

I can't help you for coding, but I have found the common format of vCard in previous post. You can try to achieve your task using this format. The left side values are always static. Hope if you can use that way.

For just reference, i found googling... (support for all 3 mobile platforms)

http://learnyii.blogspot.com/2011/04/vcard-qr-code-iphone-android-blackberry.html

Community
  • 1
  • 1
DShah
  • 9,768
  • 11
  • 71
  • 127
0
    ABMutableMultiValueRef date = ABRecordCopyValue(newPerson, kABPersonDateProperty);
    ABMultiValueAddValueAndLabel(date, dateTextField.text, kABPersonAnniversaryLabel, NULL);            
    ABRecordSetValue(newPerson, kABPersonDateProperty, date,nil);