2

QRコードvCardを読み取り、QRコードvCardの連絡先情報の詳細を表示するために使用される1つの簡単なアプリケーションを開発しています。次のように、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

この質問をするのはかなり久しぶりです。しかし、今すぐ回答することは、このページに移動した人にも役立つ可能性があります。以下の方法を参照して、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 に答える
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 に答える
0

QRコードのエンコーダーが使えます

エンコーダー

于 2013-05-01T07:40:53.520 に答える