Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
362 views
in Technique[技术] by (71.8m points)

sharing - Attach object using iOS6 UIActivityViewController

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.

The corresponding code in iOS5 is:

MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
[picker addAttachmentData:data mimeType:@"application/XXX" fileName:fileName];
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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];

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...