how to add .vcf file as attachment in application iphone

不问归期 提交于 2019-12-04 12:15:33

问题


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


回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/4303011/how-to-add-vcf-file-as-attachment-in-application-iphone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!