在 iPhone 中创建 vCard
我正在尝试在 iPhone 中创建 vCard 表示形式。我为 vCard 创建了一个字符串表示形式。我不知道如何将其转换为 NSData 表单以便将其作为附件邮寄。这是我到目前为止所拥有的:
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 表示的代码是:
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"];
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"];
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"];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
完全同意戴夫·德隆的回答。只是想补充一点,因为我只是想创建一个 vCard 文件 (.vcf) 并将其附加到应用内电子邮件操作,所以我所做的是创建一个字符串,然后创建一个临时文件,其中包含我拥有数据的所有 vCard 字段。
就我而言,我在 NSManagedObject 子类中创建了一个方法来返回 -vCardRepresentation 字符串。
Objective-c 类别或简单的框架将是一个很大的帮助 - 我可能会在适当的时候这样做。
Wikipedia 页面和 vCard (3.0) 的正式规范确实很有帮助。
更新 2:我急于完成一个应用程序,但因为我确实想创建 vCard 数据并将其作为附件添加到多个地方的应用程序内邮件消息中,所以我现在制作了一个单独的类控制器,该控制器使/隐藏vCard 语法的详细信息。然后我有一个方法返回 vCard 字符串的 NSData 版本,以将其作为附件直接添加到邮件消息中。这是一个更干净的解决方案,您不需要创建任何文件,即使它们只是临时的。另外,无论如何,您实际上并不需要数据的 NSString 表示形式,除非您确实想创建一个文件并多次使用它来保存重新制作数据?
我的控制器类有许多 -setXXXX 方法,这些方法将值添加到字典中,其中键是带有 vCard 字段名称的字符串,例如 FN、TEL 和 ADR。然后,当我调用它的 -vCardRepresentation 时,它现在返回一个 NSData 对象(通过扫描字典构建),可以直接在 MFMailComposeViewController 的 -addAttachmentData:mimeType:fileName: 方法。
如果我可以整理我的代码,使其足够通用,我会稍后发布。
更新:这是我的代码,它可能会帮助某人入门:
显然,我正在引用我的类中的属性,加上一个名为 -addressWithSeparator 的方法来构建地址的数据,其中必须包含 ;甚至对于可选地址字段也是如此。
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.
In my case I made a method in my NSManagedObject subclass to return a -vCardRepresentation string.
An Objective-c category or simple framework would be a great help - I might do it when time.
The Wikipedia page and the formal spec for vCard (3.0) really help.
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?
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:
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.