6

私のアプリでは、MFMailComposeViewControllerの添付ファイルとして.vcfファイルを追加したいと思います。

4

2 に答える 2

3

MFMailComposeViewControllerのドキュメントには、そのaddAttachmentData:mimeType:fileName:インスタンスメソッドが次の方法であることが示されています。

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

添付ファイルは、NSDataにロードまたは変換された実際のvcfファイルです。

vcfのmimeType@"text/x-vcard"です。

fileNameはどのような名前でもかまいませんが、.vcf拡張子を使用して、電子メールプログラムが確実に理解できるようにする必要があります。

于 2010-11-29T11:17:01.680 に答える
3

以下のコードを使用して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);
}
于 2013-07-03T11:37:53.443 に答える