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

时间:2023-01-19 16:54:11

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

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

2 个解决方案

#1


3  

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

MFMailComposeViewController的文档显示其addAttachmentData:mimeType:fileName:instance方法是要走的路:

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

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

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

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

vcf的mimeType是@“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.

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

#2


3  

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

使用以下代码创建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);
}

#1


3  

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

MFMailComposeViewController的文档显示其addAttachmentData:mimeType:fileName:instance方法是要走的路:

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

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

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

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

vcf的mimeType是@“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.

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

#2


3  

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

使用以下代码创建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);
}