从vCard读取/解析数据

时间:2013-09-24 08:26:39

标签: iphone ios objective-c vcard

iOS SDK中是否有可用于读取/解析vCard格式数据的框架/库? 我在NSString中以vcard格式接收数据,我必须解析它。 谷歌搜索了很多,但还没找到解决方案。

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

2 个答案:

答案 0 :(得分:6)

我确实找到了一些解决方案......

查看以下链接......

1)想要现成的示例代码==> Click here

2)写入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)导入vCard示例代码==> Click here

4)用于创建自定义解析器==> Click here

答案 1 :(得分:4)

OS X v10.11和iOS 9引入了CNContactVCardSerialization,这使得解析VCard变得轻而易举。