我正在迁移使用UIActivityViewController在iOS6中进行共享,但我无法弄清楚如何通过电子邮件共享创建要包含的电子邮件附件对象.
iOS5中的相应代码是:
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init]; [picker addAttachmentData:data mimeType:@"application/XXX" fileName:fileName];
解决方法
你对UIActivityViewController的控制非常有限,但是如果你附加了众所周知的mime类型,我发现你可以通过在文件URL中提供相关的文件扩展名来使它正常工作.例如,如果您的附件是vCard,请在文件URL中使用“.vcf”扩展名:
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];