如何在 iPhone 应用程序中添加 .vcf 文件作为附件

发布于 2024-10-04 22:44:30 字数 64 浏览 4 评论 0原文

在我的应用程序中,我想添加 .vcf 文件作为 MFMailComposeViewController 中的附件。

In my app I want to add .vcf file as attachment in MFMailComposeViewController.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓦然回首 2024-10-11 22:44:30

MFMailComposeViewController 文档显示它的 addAttachmentData:mimeType:fileName: 实例方法是正确的方法:

- (void)addAttachmentData:(NSData*)attachment mimeType:(NSString*)mimeType fileName:(NSString*)filename

附件是加载或转换为 NSData 的实际 vcf 文件。

vcf 的mimeType@"text/x-vcard"

fileName 是您想要的任何名称,但您应该使用 .vcf 扩展名来确保电子邮件程序能够理解它。

The documentation for MFMailComposeViewController shows that its addAttachmentData:mimeType:fileName: instance method is the way to go:

- (void)addAttachmentData:(NSData*)attachment mimeType:(NSString*)mimeType fileName:(NSString*)filename

The attachment is the actual vcf file loaded or transformed into NSData.

The mimeType for a vcf is @"text/x-vcard".

The fileName is whatever you want to call it, though you should uses the .vcf extension to ensure that email programs will understand it.

自我难过 2024-10-11 22:44:30

使用下面的代码创建一个 vcf 文件并将其保存在目录中。

ABAddressBookRef addressBook = ABAddressBookCreate();

//-------------------Creating and saving vcf --------------------------------
CFArrayRef contacts = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFDataRef vcards = (CFDataRef)ABPersonCreateVCardRepresentationWithPeople(contacts);
NSString *vcardString = [[NSString alloc] initWithData:(NSData *)vcards encoding:NSUTF8StringEncoding];
NSError *error;
NSFileManager *fileMgr = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *folderPath = [paths objectAtIndex:0];
NSString *filePath = [folderPath stringByAppendingPathComponent:@"contacts.vcf"];
[vcardString writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
NSLog(@"Documents directory: %@",[fileMgr contentsOfDirectoryAtPath: folderPath error:&error]);
if (addressBook) {
    CFRelease(addressBook);
}

Use the below code to create a vcf file and save it in directory.

ABAddressBookRef addressBook = ABAddressBookCreate();

//-------------------Creating and saving vcf --------------------------------
CFArrayRef contacts = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFDataRef vcards = (CFDataRef)ABPersonCreateVCardRepresentationWithPeople(contacts);
NSString *vcardString = [[NSString alloc] initWithData:(NSData *)vcards encoding:NSUTF8StringEncoding];
NSError *error;
NSFileManager *fileMgr = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *folderPath = [paths objectAtIndex:0];
NSString *filePath = [folderPath stringByAppendingPathComponent:@"contacts.vcf"];
[vcardString writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
NSLog(@"Documents directory: %@",[fileMgr contentsOfDirectoryAtPath: folderPath error:&error]);
if (addressBook) {
    CFRelease(addressBook);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文