how to add .vcf file as attachment in application iphone
Asked Answered
C

2

6

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

Cristencristi answered 29/11, 2010 at 10:51 Comment(1)
how to make a vcf file in iphone for all contact pls help me .Caudate
T
3

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.

Tirpitz answered 29/11, 2010 at 11:17 Comment(1)
how to make a vcf file in iphone for all contact pls help me .Caudate
F
3

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);
}
Friedland answered 3/7, 2013 at 11:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.