使用iOS6 UIActivityViewController附加对象 [英] Attach object using iOS6 UIActivityViewController
问题描述
我正在迁移使用UIActivityViewController在iOS6中共享,但我无法弄清楚如何在通过电子邮件共享时创建电子邮件附件对象。
I'm migrating to use the UIActivityViewController for sharing in iOS6, but I can't figure out how to create email attachment objects to be included when sharing by email.
iOS5中的相应代码是:
The corresponding code in iOS5 is:
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
[picker addAttachmentData:data mimeType:@"application/XXX" fileName:fileName];
推荐答案
你对UIActivityViewController的控制非常有限,但如果你通过提供知名的mime类型,我发现你可以通过在文件URL中提供相关的文件扩展名来使其正常工作。例如,如果您的附件是vCard,请在文件URL中使用.vcf扩展名:
You have very limited control over UIActivityViewController, but if you're attaching well-know mime types, I found you can get it to work correctly by providing the associated file extension in a file URL. For example, if your attachment is a vCard, use the ".vcf" extension in the file URL:
NSString *docsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
// The file extension is important so that some mime magic happens!
NSString *filePath = [docsPath stringByAppendingPathComponent:@"vcard.vcf"];
NSURL *fileUrl = [NSURL fileURLWithPath:filePath];
[data writeToURL:fileUrl atomically:YES]; // save the file
// Now pass the file URL in the activity items array
UIActivityViewController *avc = [[UIActivityViewController alloc] initWithActivityItems:
@[@"Here's an attached vCard", fileUrl] applicationActivities:nil];
[vc presentModalViewController:avc animated:YES];
这篇关于使用iOS6 UIActivityViewController附加对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!