iOS development-Generate/parse. vcf files

Source: Internet
Author: User
Tags vcard

VCF, a format for address book export.


One. Generate VCF File

Suppose you want to generate the VCF format file for the data in our iphone address Book.

We can help with icloud.

Tips: How to export iphone contacts via icloud of course, suppose you want to use code generation in your app, and you can. Pilot out the Address book data, then parse, and then generate the VCF file can be.

Refer to the following code:

-(nsstring*) Generatevcardstringwithcontacts: (cfarrayref) Contacts {Nsinteger counter = 0;        NSString *vcard = @ ""; for (Cfindex i = 0; i < Cfarraygetcount (contacts), i++) {abrecordref person = Cfarraygetvalueatindex (con                Tacts, i);        NSString *firstname = (nsstring*) abrecordcopyvalue (person, kabpersonfirstnameproperty); FirstName = (firstName?

FirstName: @ ""); NSString *lastname = (NSString *) abrecordcopyvalue (person, kabpersonlastnameproperty); LastName = (LastName lastName: @ ""); NSString *middlename = (NSString *) abrecordcopyvalue (person, kabpersonmiddlenameproperty); NSString *prefix = (NSString *) abrecordcopyvalue (person, kabpersonprefixproperty); NSString *suffix = (NSString *) abrecordcopyvalue (person, kabpersonsuffixproperty); NSString *nickname = (NSString *) abrecordcopyvalue (person, kabpersonnicknameproperty); NSString *firstnamephonetic = (NSString *) abrecordcopyvalue (person, kabpersonfirstnamephoneticproperty); NSString *lastnamephonetic = (NSString *) abrecordcopyvalue (person, kabpersonlastnamephoneticproperty); NSString *organization = (NSString *) abrecordcopyvalue (person, kabpersonorganizationproperty); NSString *jobtitle = (NSString *) abrecordcopyvalue (person, kabpersonjobtitleproperty); NSString *department = (NSString *)Abrecordcopyvalue (person, kabpersondepartmentproperty); NSString *compositename = [NSString stringwithformat:@ "%@%@", Firstname,lastname]; if (i > 0) {vcard = [vcard stringbyappendingformat:@ ' \ n ']; } vcard = [vcard stringbyappendingformat:@ ' begin:vcard\nversion:3.0\nn:%@;%@;%@;%@;%@\n ', ( FirstName?

FirstName: @ ""), (LastName lastName: @ "), (middlename? MiddleName: @" "), (prefix?)

Prefix: @ ""), (suffix suffix: @ "")]; vcard = [vcard stringbyappendingformat:@ "fn:%@\n", Compositename]; if (nickname) vcard = [vcard stringbyappendingformat:@ "nickname:%@\n", nickname]; if (firstnamephonetic) vcard = [vcard stringbyappendingformat:@ "x-phonetic-first-name:%@\n", firstnamephonetic]; if (lastnamephonetic) vcard = [vcard stringbyappendingformat:@ "x-phonetic-last-name:%@\n", lastnamephonetic]; Work if (organization) vcard = [vcard stringbyappendingformat:@ "org:%@;%@\n", (organization?

organization:@ ""), (department?department:@ "")]; if (jobtitle) vcard = [vcard stringbyappendingformat:@ "title:%@\n", JobTitle]; Mail Abmultivalueref emails = abrecordcopyvalue (person, kabpersonemailproperty); if (emails) {for (int k = 0; k < Abmultivaluegetcount (emails); k++) {NSString *label = (NSSt ring*) Abaddressbookcopylocalizedlabel (Abmultivaluecopylabelatindex (emails, k)); NSString *email = (NSString *) Abmultivaluecopyvalueatindex (emails, k); NSString *labellower = [Label lowercasestring]; vcard = [vcard stringbyappendingformat:@ "email;type=internet;type=work:%@\n", EMAIL]; if ([Labellower isequaltostring:@ "Home"]) vcard = [vcard stringbyappendingformat:@ "email;type=internet;type=home:%@\ n ", email]; else if ([Labellower isequaltostring:@ "work"]) vcard = [vcard stringbyappendingformat:@ "email;type=internet; type=work:%@\n ", email]; else {//type counter++ not resolved; vcard = [vcard stringbyappendingformat:@ "item%d.email;type=internet:%@\nitem%d.x-ablabel:%@\n", Counter,email, Counter,label]; }}}//Tel abmultivalueref phonenumbers = abrecordcopyvalue (person, Kabpersonpho Neproperty); if (phonenumbers) {for (int k = 0; k < Abmultivaluegetcount (phonenumbers); k++) {NSString *l Abel = (nsstring*) Abaddressbookcopylocalizedlabel (Abmultivaluecopylabelatindex (Phonenumbers, k)); NSString *number = (NSString *) Abmultivaluecopyvalueatindex (phonenumbers, k); NSString *labellower = [Label lowercasestring]; if ([Labellower isequaltostring:@ "mobile"]) vcard = [vcard stringbyappendingformat:@ "tel;type=cell:%@\n", number]; else if ([Labellower isequaltostring:@ "Home"]) vcard = [vcard stringbyappendingformat:@ "tel;type=home:%@\n", number]; else if ([Labellower isequaltostring:@ "work"]) vcard = [vcard stringbyappendingformat:@ "tel;type=work:%@\n", number]; else if ([Labellower isequaltostring:@ "main"]) vcard = [vcard stringbyappendingformat:@ "tel;type=main:%@\n", num BER]; else if ([Labellower isequaltostring:@ "Homefax"]) vcard = [vcard stringbyappendingformat:@ "tel;type=home;type=fax:%@\ N ", number]; else if ([Labellower isequaltostring:@ "Workfax"]) vcard = [vcard stringbyappendingformat:@ "tel;type=work;type=fax:%@\ N ", number]; else if ([Labellower isequaltostring:@ "Pager"]) vcard = [vcard stringbyappendingformat:@ "tel;type=pager:%@\n", number] ; else if ([Labellower isequaltostring:@ "Other"]) vcard = [vcard stringbyappendingformat:@ "tel;type=other:%@\n", number] ; else {//type counter++ not resolved; vcard = [vcard stringbyappendingformat:@] Item%d.tel:%@\nitEm%d.x-ablabel:%@\n ", Counter,number,counter,label]; }}}//address Abmultivalueref address = Abrecordcopyvalue (person, kabpersonaddr Essproperty); if (address) {for (int k = 0; k < Abmultivaluegetcount (address); k++) {NSString *label = (NS string*) Abaddressbookcopylocalizedlabel (Abmultivaluecopylabelatindex (label, k)); Nsdictionary *dic = (nsdictionary *) Abmultivaluecopylabelatindex (address, k); NSString *labellower = [Label lowercasestring]; nsstring* country = [dic valueforkey: (NSString *) Kabpersonaddresscountrykey]; nsstring* city = [dic valueforkey: (NSString *) Kabpersonaddresscitykey]; nsstring* state = [dic valueforkey: (NSString *) Kabpersonaddressstatekey]; nsstring* Street = [dic valueforkey: (NSString *) Kabpersonaddressstreetkey]; nsstring* zip = [dic valueforkey: (NSString *) KabpersonadDresszipkey]; nsstring* CountryCode = [dic valueforkey: (NSString *) Kabpersonaddresscountrycodekey]; NSString *type = @ ""; NSString *labelfield = @ ""; counter++; if ([Labellower isequaltostring:@ "Work"]) type = @ "Work"; else if ([Labellower isequaltostring:@ "Home"]) type = @ "Home"; else if (label && [Label length] > 0) {labelField = [NSString Stringwithforma t:@ "item%d.x-ablabel:%@\n", Counter,label]; } vcard = [vcard stringbyappendingformat:@ "item%d.adr;type=%@:;; %@;%@;%@;%@;%@\n% @item%d.x-abadr:%@\n ", Counter, type, (Street Street: @ "), (City: @"), (State state: @ ""), (Zip Zip: @ ""), (country CoUntry: @ ""), LabelField, counter, (CountryCode?

CountryCode: @ "")]; }}//The rest is not used very often, I will not write, if required. Self-completion//URL//TODO://IM//TODO://Photo//TODO: vcard = [vcard stringbyappendingstring:@ "End:vcard"]; } return vcard;}




Two.

Parse VCF file

Use Uidocumentinteractioncontroller to show this file with Quicklook.

Then he will parse it on his own initiative and have the Add to Address Book option.


Of course. We are also able to parse it ourselves.

Detailed code such as the following:

Parsing vcf-(void) parsevcardstring: (nsstring*) vcardstring{nsarray *lines = [vcardstring componentsseparatedbystring:@]        \ n "];         for (nsstring*. Lines) {if ([line hasprefix:@ "BEGIN"]) {NSLog (@ "parse start");        } else if ([Line hasprefix:@ "End"]) {NSLog (@ "Parse END"); } else if ([line hasprefix:@ ' N: "]) {nsarray *uppercomponents = [Line componentsseparatedbystring            :@":"];                        Nsarray *components = [[uppercomponents objectatindex:1] componentsseparatedbystring:@ ";"];            NSString * LastName = [Components objectatindex:0];                        NSString * FirstName = [Components objectatindex:1];                    NSLog (@ "name%@%@", lastname,firstname);        } else if ([line hasprefix:@ "EMAIL;"])            {Nsarray *components = [Line componentsseparatedbystring:@ ":"]; NSString *emailaddress = [Components objectatindex:1];                    NSLog (@ "EmailAddress%@", EmailAddress);        } else if ([line hasprefix:@ "TEL;"])            {Nsarray *components = [Line componentsseparatedbystring:@ ":"];            NSString *phonenumber = [Components objectatindex:1];        NSLog (@ "PhoneNumber%@", PhoneNumber); }    }    }


iOS development-Generate/parse. vcf files

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.