从AddressBook.framework生成vCard [英] Generate vCard from AddressBook.framework

查看:138
本文介绍了从AddressBook.framework生成vCard的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的iPhone应用程序中使用了AddressBook.framework,我想在Contacts.app中复制一些分享功能。这基本上附加到电子邮件的特定联系人vCard。据我所知,文档中没有提到生成vCard的内容。



这是自己生成一个的情况吗?

解决方案

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



在我的例子中,我在 NSManagedObject 子类中创建了一个方法来返回 vCardRepresentation string。



Objective-c类别或简单的框架将是一个很大的帮助 - 我可以做时间。

维基百科页面和正式规范< a href =http://www.rfc-editor.org/rfc/rfc2426.txt =nofollow noreferrer> vCard (3.0)真的有帮助。



UPDATE 3 :如果您使用iOS 5或更高版本,现在在 ABPerson ,可返回给定人员记录的vCard表示形式:

  CFDataRef ABPersonCreateVCardRepresentationWithPeople(CFArrayRef people); 

UPDATE 2 :我急于取得应用程式完成,因为我想要创建vCard数据并将其作为附件添加到应用程序内邮件消息的几个地方,我现在做了一个单独的类控制器,使/ v隐藏vCard语法的细节。然后我只有一个方法返回一个NSData版本的vCard字符串作为附件直接添加到邮件消息。这是一个更清洁的解决方案,你不需要创建任何文件,即使他们只是暂时的。此外,你真的不需要一个NSString表示的数据,除非你想创建一个文件,并多次使用它来保存重新制作数据?



我的控制器类有一些 -setXXXX 方法添加值到字典,其中的键是与vCard字段名称,如FN,TEL和ADR的字符串。然后当我调用它的 -vCardRepresentation ,现在返回一个NSData对象(通过扫描字典构建),可以直接在MFMailComposeViewController的 - addAttachmentData :mimeType:fileName:方法。



如果我可以整理我的代码,所以它是通用的,我会后来发布。 b
$ b

UPDATE :这是我的代码,它可能有助于某人开始:

   - (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:@\\\
];

[mutableArray release];

return string;
}

显然,我引用了我的类中的属性, -addressWithSeparator 构建地址的数据,其中必须包括;即使对于可选地址字段也是如此。


I'm utilising the AddressBook.framework in my iPhone app, and I'd like to replicate something along the lines of the share feature in Contacts.app. This basically attach's a specific contacts vCard to an email. As far as I know, there is nothing in the documentation that mentions generating a vCard.

Is this a case of generating one myself? Or is there something available that can help me?

解决方案

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 3: If you're using iOS 5 or later there's now a function in ABPerson that can return the vCard representation of the given person records:

CFDataRef ABPersonCreateVCardRepresentationWithPeople(CFArrayRef people);

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:

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

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.

这篇关于从AddressBook.framework生成vCard的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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