在iPhone中创建vCard [英] Creating a vCard in iPhone

查看:140
本文介绍了在iPhone中创建vCard的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在iPhone中创建vCard表示形式.我为电子名片创建了字符串表示形式.我不确定如何将其转换为NSData形式以作为附件邮寄.这是我到目前为止的内容:

I'm trying to create a vCard representation in the iPhone. I created a string representation for the vCard. I'm not sure how to convert it into the NSData form in order to mail it as an attachment. This is what I have so far:

        NSString *vCardString = [vCard getVCFString]; // returns string representation for vCard
        NSData *vCardData = [vCardString dataUsingEncoding:NSUTF8StringEncoding];
        [mailController addAttachmentData:vCardData mimeType:@"text/x-vcard" fileName:@"LocationInfo"];

当我在发送测试电子邮件时单击附件时,它将转到创建新联系人/添加为现有联系人.由于iPhone将其识别为联系人,这是否正确?我想我对从中获取位置信息很感兴趣,但这似乎没有出现在我的附件中.创建我的VCF表示形式的代码是:

When I click on the attachment when I do a test email, it goes to the create new contact/add as existing contact. Is this correct since the iPhone recognizes it as a contact? I guess I was interested in getting the location information out of it, but that didn't seem to show up in my attachment. The code for creating my VCF representation is:

vcfString = [[NSMutableString allocWithZone:[self zone]] initWithCapacity:kDefaultStringSize];
    [vcfString appendString:@"BEGIN:VCARD\n"];
    [vcfString appendString:@"VERSION:3.0\n"];
    if (s) {
        NSMutableString *aString = [[NSMutableString alloc] initWithFormat:@"%@;", s];
        [vcfString appendString:aString];
        [aString replaceOccurrencesOfString:@";" withString:@"" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [aString length])];
        street = [[NSString allocWithZone:[self zone]] initWithString:aString];
        [aString release];
    }
    if (c) {
        NSMutableString *aString = [[NSMutableString alloc] initWithFormat:@"%@;", c];
        [vcfString appendString:aString];
        [aString replaceOccurrencesOfString:@";" withString:@"" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [aString length])];
        city = [[NSString allocWithZone:[self zone]] initWithString:aString];
        [aString release];
    }
    if (st) {
        NSMutableString *aString = [[NSMutableString alloc] initWithFormat:@"%@;", st];
        [vcfString appendString:aString];
        [aString replaceOccurrencesOfString:@";" withString:@"" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [aString length])];
        state = [[NSString allocWithZone:[self zone]] initWithString:aString];
        [aString release];
    }
    // TODO: look up semicolon for VCard representation
    if (z) {
        NSString *aString = [[NSString alloc] initWithFormat:@"%@\n", z];
        [vcfString appendString:aString];
        zip = [[NSString allocWithZone:[self zone]] initWithString:aString];
        [aString release];
    }
    [vcfString appendString:@"END:VCARD"];

推荐答案

完全同意Dave DeLong的回答.只是要添加一下,因为我只是想创建一个vCard文件(.vcf),并将其附加到应用内电子邮件操作中,所以我要做的是创建一个字符串,然后创建一个临时文件,其中所有vCard字段都有我要的数据

Totally agree with Dave DeLong's answer. Just want to add that since I simply wanted to create a vCard file (.vcf) and attach it to an in-app email action what I did was create a string, then a temporary file, with all the vCard fields I had data for.

在我的情况下,我在NSManagedObject子类中创建了一个方法来返回-vCardRepresentation字符串.

In my case I made a method in my NSManagedObject subclass to return a -vCardRepresentation string.

Objective-c类别或简单框架会很有帮助-我可能会在有时间的时候这样做.

An Objective-c category or simple framework would be a great help - I might do it when time.

维基百科页和vCard(3.0)的正式规范确实很有帮助.

The Wikipedia page and the formal spec for vCard (3.0) really help.

更新2:我急于完成一个应用程序,但是因为我确实想创建vCard数据并将其作为附件添加到应用程序内邮件中的多个位置,所以我现在制作了一个单独的类控制器, /隐藏vCard语法的详细信息.然后,我只有一个方法,该方法返回vCard字符串的NSData版本,以附件形式直接添加到邮件中.这是一个更干净的解决方案,即使它们只是临时的,也不需要创建任何文件.再加上您真的不需要数据的NSString表示形式,除非您确实想要创建一个文件并多次使用它来保存重新制作数据?

UPDATE 2: I'm rushing to get an app complete, but because I did want to create vCard data and add it as attachments to in-app mail messages in several places I've now made a separate class controller that makes / hides the details of the vCard syntax. Then I just have a method that returns an NSData version of the vCard string to add directly to a mail message as an attachment. This is a much cleaner solution, you don't need to create any files, even if they were only temporary. Plus you don't really need an NSString representation of the data anyway, unless you did want to create a file and use it more than once to save re-making the data?

我的控制器类具有许多-setXXXX方法,这些方法将值添加到字典中,其中的键是带有vCard字段名称的字符串,例如FN,TEL和ADR.然后,当我调用它的-vCardRepresentation时,它现在返回一个NSData对象(通过扫描字典来构建),该对象可以直接在MFMailComposeViewController的

My controller class has a number of -setXXXX methods that add values to a dictionary where the keys are strings with the vCard field names, like FN, TEL and ADR. Then when I call its -vCardRepresentation, this now returns an NSData object (built by scanning the dictionary) that can be used directly in MFMailComposeViewController's -addAttachmentData:mimeType:fileName: method.

如果我可以整理代码,那么它已经足够通用了,我稍后再发布.

If I can tidy my code, so it's generic enough, I'll post it later.

更新:这是我的代码,它可以帮助某人入门:

UPDATE: Here's my code, it might help someone get started:

- (NSString *)vCardRepresentation
{
  NSMutableArray *mutableArray = [[NSMutableArray alloc] init];

  [mutableArray addObject:@"BEGIN:VCARD"];
  [mutableArray addObject:@"VERSION:3.0"];

  [mutableArray addObject:[NSString stringWithFormat:@"FN:%@", self.name]];

  [mutableArray addObject:[NSString stringWithFormat:@"ADR:;;%@",
                           [self addressWithSeparator:@";"]]];

  if (self.phone != nil)
    [mutableArray addObject:[NSString stringWithFormat:@"TEL:%@", self.phone]];

  [mutableArray addObject:[NSString stringWithFormat:@"GEO:%g;%g",
                           self.latitudeValue, self.longitudeValue]];

  [mutableArray addObject:[NSString stringWithFormat:@"URL:http://%@",
                           self.website]];

  [mutableArray addObject:@"END:VCARD"];

  NSString *string = [mutableArray componentsJoinedByString:@"\n"];

  [mutableArray release];

  return string;
}

很明显,我在引用类中的属性,外加一个名为-addressWithSeparator的方法来建立地址数据,该方法必须包含;.分隔符,甚至用于可选的地址字段.

Obviously I'm making reference to properties in my class, plus a method called -addressWithSeparator to build-up the data for the address, which has to include the ; separator even for optional address fields.

这篇关于在iPhone中创建vCard的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆