iOS development-Generate/parse. vcf files

Source: Internet
Author: User

VCF, a format for address book export.


One. Generate VCF File

If you want to put the data in our iphone Address Book, generate the VCF format file.

We can help with icloud.

Tip: How to export iphone contacts via icloud of course, if you want to use code generation in your app, it's perfectly possible. Pilot out the Address book data, then parse, and then generate the VCF file.

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:@ ""), (d                epartment?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 *label = (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, Kabperson        Addressproperty); 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 stringwithformat:@ "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: @ ""), Labe                         Lfield, Counter, (CountryCode CountryCode: @ "")            ]; }}//The remainder is not used, I will not write, if necessary, self-completion//URL//TODO://IM//T    ODO://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 automatically and have the Add to Address Book option.


Of course, we can also parse it ourselves.

The specific code is as follows:

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); }    }    }


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.