How to add .vcf file as application in iphone application

In my application, I want to add a .vcf file as an attachment to MFMailComposeViewController.

+6
objective-c iphone attachment mfmailcomposeviewcontroller vcf
source share
2 answers

The documentation for MFMailComposeViewController shows that the instance method addAttachmentData: mimeType: file_name: is the way:

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

An attachment is an actual vcf file downloaded or converted to NSData.

MimeType for vcf - @ "text / x-vcard" .

The file name is what you want to name, although you should use the .vcf extension to make sure that mailers understand it.

+3
source share

Use the code below to create a vcf file and save it to a 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); } 
+3
source share

All Articles