2

我正在开发一个简单的应用程序,用于读取 QR 码 vCard 并在 QR 码 vCard 中显示联系信息的详细信息。我可以扫描 vCard 并获取有关联系人的详细信息,如下所示

BEGIN:VCARD
VERSION:2.1
N:XX;XXXXXXXX
FN:XXXXXXXXX XX
TEL;WORK;VOICE:91999999999
EMAIL;WORK;INTERNET:sac@gmail.com
END:VCARD

但我需要解析其中的每个元素。如何解析细节,解析方式有哪些?

4

4 回答 4

1

文档指向此方法:

- (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];    
    }    

}

我会先看看它返回什么也许尝试循环字典以获取其他数据

于 2013-04-30T22:56:18.783 回答
1

好吧,问这个问题已经很长时间了。但现在回答也可能对导航到此页面的人有所帮助。您可以使用以下方法作为参考来解析 VCF 数据。

    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] );
于 2014-07-23T04:35:01.740 回答
0

你可以使用二维码的编码器

编码器

于 2013-05-01T07:40:53.520 回答
0

您可以使用CNContactVCardSerializationCNContact从数据中获取对象

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)")
       }
    }
于 2017-12-18T04:55:42.650 回答