sachi
sachi

Reputation: 2132

iPhone : How to get each element from QR code vCard?

I am developing one simple application which is used to read the QR code vCard and display the details of the contact information in the QR code vCard. I am able to scan the vCard and get the details about the contact as follows

BEGIN:VCARD
VERSION:2.1
N:XX;XXXXXXXX
FN:XXXXXXXXX XX
TEL;WORK;VOICE:91999999999
EMAIL;WORK;INTERNET:[email protected]
END:VCARD

But I need to parse each element in it. How to parse the details and what are the ways to parse?

Upvotes: 2

Views: 2018

Answers (4)

Lal Krishna
Lal Krishna

Reputation: 16180

You can use CNContactVCardSerialization to get CNContact Object from data

import Contacts
   if let data = str.data(using: .utf8) {
       do {
          let contacts = try CNContactVCardSerialization.contacts(with: data)
          let contact = contacts.first
          print("\(String(describing: contact?.familyName))")
          return contact
       } catch {
          print("Contact Error: \(error.localizedDescription)")
       }
    }

Upvotes: 0

Xreen
Xreen

Reputation: 21

Well its being quite a long time that this question is asked. But answering now might also help someone who is navigated to this page. You can use the below method for reference to parse the VCF data.

    NSString *str = [NSString stringWithString:response.vcfString];
    NSArray *subStrings = [str componentsSeparatedByCharactersInSet:[NSCharacterSet      characterSetWithCharactersInString:@"\n"]];
    NSArray *getData = [[NSArray alloc]init];

    NSString *arr = @"";

    for (int i=0;i<[subStrings count];i++)
    {
        arr = [subStrings objectAtIndex:i];

        NSArray *abc = [arr componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@";"]];

        if([[abc objectAtIndex:0] isEqualToString:@"FN"])
        {
            getData=[[abc objectAtIndex:1] componentsSeparatedByCharactersInSet: [NSCharacterSet characterSetWithCharactersInString:@":"] ];

            if([getData count] > 1)
            fullName = [getData objectAtIndex:1];
        }
        else if([[abc objectAtIndex:0] isEqualToString:@"N"])
        {
            getData=[[abc objectAtIndex:1] componentsSeparatedByCharactersInSet:  [NSCharacterSet characterSetWithCharactersInString:@":"]];

            if([getData count] > 1)
            lastName = [getData objectAtIndex:1];
            FirstName = [abc objectAtIndex:2];
        }
        else if([[abc objectAtIndex:0] isEqualToString:@"TITLE"])
        {
            getData=[[abc objectAtIndex:1] componentsSeparatedByCharactersInSet:  [NSCharacterSet characterSetWithCharactersInString:@":"]];

            if([getData count] > 1)
            title = [getData objectAtIndex:1];
        }
        else if([[abc objectAtIndex:0] isEqualToString:@"TEL"])
        {
            NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
            if([abc count] == 3)
            {
                getData=[[abc objectAtIndex:2] componentsSeparatedByCharactersInSet: [NSCharacterSet characterSetWithCharactersInString:@":"]];

                if([[abc objectAtIndex:1] isEqualToString:@"WORK"])
                {
                    if([getData count] > 1)
                        [dict setObject:[getData objectAtIndex:1] forKey:@"Tel"];
                }
                else if([[abc objectAtIndex:1] isEqualToString:@"CELL"])
                {
                    if([getData count] > 1)
                        [dict setObject:[getData objectAtIndex:1] forKey:@"Tel"];
                }
            }
            else if([abc count] == 4)
            {
                getData=[[abc objectAtIndex:3] componentsSeparatedByCharactersInSet: [NSCharacterSet characterSetWithCharactersInString:@":"]];

                if([[abc objectAtIndex:1] isEqualToString:@"WORK"] && [[abc objectAtIndex:2] isEqualToString:@"FAX"])
                {
                    if([getData count] > 1)
                        [dict setObject:[getData objectAtIndex:1] forKey:@"Tel"];
                }
            }
            else
            {
                getData=[[abc objectAtIndex:1] componentsSeparatedByCharactersInSet:  [NSCharacterSet characterSetWithCharactersInString:@":"]];

                if([[getData objectAtIndex:0] isEqualToString:@"WORK"])
                {
                    if([getData count] > 1)
                        [dict setObject:[getData objectAtIndex:1] forKey:@"Tel"];
                }
                else if([[getData objectAtIndex:0] isEqualToString:@"FAX"])
                {
                    if([getData count] > 1)
                        [dict setObject:[getData objectAtIndex:1] forKey:@"Tel"];
                }
            }
            [_telList addObject:dict];
        }
        else if([[abc objectAtIndex:0] isEqualToString:@"EMAIL"])
        {
            NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];

            if([abc count] == 3)
            {
                getData=[[abc objectAtIndex:2] componentsSeparatedByCharactersInSet: [NSCharacterSet characterSetWithCharactersInString:@":"]];

                if([[abc objectAtIndex:1] isEqualToString:@"WORK"])
                {
                    if([getData count] > 1)
                        [dict setObject:[getData objectAtIndex:1] forKey:@"Email"];
                }
            }
            else
            {
                getData=[[abc objectAtIndex:1] componentsSeparatedByCharactersInSet: [NSCharacterSet characterSetWithCharactersInString:@":"]];

                if([getData count] > 1)
                    [dict setObject:[getData objectAtIndex:1] forKey:@"Email"];
            }
            [_emailList addObject:dict];
        }
        else if([[abc objectAtIndex:0] isEqualToString:@"ORG"])
        {
            NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];

            if([abc count] == 5)
            {
                getData=[[abc objectAtIndex:2] componentsSeparatedByCharactersInSet: [NSCharacterSet characterSetWithCharactersInString:@":"]];

                if([[abc objectAtIndex:1] isEqualToString:@"WORK"])
                {
                    NSString *orgStr = @"";
                    if([getData count] > 1)
                    {
                        orgStr = [getData objectAtIndex:1];
                    }
                    [dict setObject:[orgStr stringByAppendingString:[abc objectAtIndex:4]] forKey:@"Org"];
                }
            }
            else
            {
                getData=[[abc objectAtIndex:1] componentsSeparatedByCharactersInSet:  [NSCharacterSet characterSetWithCharactersInString:@":"]];

                if([getData count] > 1)
                    [dict setObject:[getData objectAtIndex:1] forKey:@"Org"];
            }
            [_orgList addObject:dict];
        }
        else if([[abc objectAtIndex:0] isEqualToString:@"ADR"])
        {
            NSMutableDictionary *dict = [NSMutableDictionary dictionary];
            if([abc count] == 9)
            {
                [dict setObject:[abc objectAtIndex:4] forKey:@"Add"];
                [dict setObject:[abc objectAtIndex:8] forKey:@"Country"];
                [dict setObject:[abc objectAtIndex:7] forKey:@"Zip"];
                [dict setObject:[abc objectAtIndex:5] forKey:@"City"];
            }
            else
            {

            }

            [_addrList addObject:dict];
        }
        else if([[abc objectAtIndex:0] isEqualToString:@"URL"])
        {
            NSMutableDictionary *dict = [NSMutableDictionary dictionary];
            if([abc count] == 3)
            {
                getData=[[abc objectAtIndex:2] componentsSeparatedByCharactersInSet: [NSCharacterSet characterSetWithCharactersInString:@":"]];

                if([getData count] > 1)
                    [dict setObject:[getData objectAtIndex:1] forKey:@"Url"];
            }
            else
            {
                getData=[[abc objectAtIndex:1] componentsSeparatedByCharactersInSet: [NSCharacterSet characterSetWithCharactersInString:@":"]];

                if([getData count] > 1)
                    [dict setObject:[getData objectAtIndex:1] forKey:@"Url"];
            }

            [_urlList addObject:dict];
        }
        else
        {

        }
    }


    NSLog(@"Details:  \nFull Name - %@\nLast Name - %@\nFirst Name - %@\ntitle - %@\nEmail - %@\norg - %@\nAddress - %@\nUrl - %@", fullName,lastName,FirstName,title,[[_emailList valueForKey:@"description"] componentsJoinedByString:@""],[_orgList description],[_addrList description],[_urlList description] );

Upvotes: 1

SAMIR RATHOD
SAMIR RATHOD

Reputation: 3506

you can use Encoder of QRCode

Encoder

Upvotes: 0

Shams Ahmed
Shams Ahmed

Reputation: 4513

The documentation points to this method:

- (void) imagePickerController: (UIImagePickerController*) reader
 didFinishPickingMediaWithInfo: (NSDictionary*) info
{
    id<NSFastEnumeration> results = [info objectForKey: ZBarReaderControllerResults];

    ZBarSymbol *symbol = nil;

    for(symbol in results){

        NSString *upcString = symbol.data;

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Scanned UPC" message:[NSString stringWithFormat:@"The UPC read was: %@", upcString] delegate:self cancelButtonTitle:nil otherButtonTitles:@"Ok", nil];

        [alert show];

        [reader dismissModalViewControllerAnimated: YES];    
    }    

}

i would first see what it returns maybe try looping the dictionary for additional data

Upvotes: 1

Related Questions