3

Is there any framework/library available in iOS SDK to read/parse data in vCard format? I am receiving data in a vcard format in a NSString and I have to parse it. Googled a lot, but couldn't find solution yet.

BEGIN:VCARD
VERSION:2.1
N:LastName;FirstName;MiddleName;Prefix;Sufix
ADR;TYPE=HOME: postbox;street2;street1;city;state;zip;country
BDAY:2010-08-19
END:VCARD
Taimur Ajmal
  • 2,778
  • 6
  • 39
  • 57

2 Answers2

6

I did found some solutions for you...

Have a look at the following links...

1) Want ready made sample code==>Click here

2) Writing to Vcard==>Click here

Code Relevent to you:

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 

3) Importing vCard sample code==>Click here

4) For creating custom parser ==> Click here

Community
  • 1
  • 1
Master Stroke
  • 5,108
  • 2
  • 26
  • 57
  • @Puneed Kamath Thanks a lot the detailed response and ref codes. I have used vCardImporter. The issue is I dont want to save anything in my Contacts App using Address Book. I just want to parse and get desired values like this. vCard.FName // will return First Name vCard.Phone // will return me Phone. do u have idea how can i do it ? – Taimur Ajmal Sep 24 '13 at 10:41
  • @TaimurAjmal..wish i would have had my mac with me now...i could have wrote code for you..but partially i can guide you... check it==>http://softsolutions.blog.com/2011/07/21/vcard-version-2-1-parser/ – Master Stroke Sep 24 '13 at 10:56
  • if([[abc objectAtIndex:1] isEqualToString:@”WORK”]) { } – Master Stroke Sep 24 '13 at 10:58
4

OS X v10.11 and iOS 9 introduces CNContactVCardSerialization which makes parsing a VCard a breeze.

  • heads up! iOs does not support unicode out of the box. We will need to modify the qrcode in order to accept unicode characters. https://stackoverflow.com/questions/46287505/cncontact-encoding-of-properties – Saqib Saud Dec 18 '18 at 08:05