使用iOS6 UIActivityViewController附加对象

我正在迁移使用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];

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...