Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Attach object using iOS6 UIActivityViewController

Tags:

ios6

sharing

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];
like image 301
Setomidor Avatar asked Sep 28 '12 19:09

Setomidor


2 Answers

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];
like image 55
markiv Avatar answered Oct 29 '22 11:10

markiv


For anyone wondering why their files aren't being shared using UIActivityViewController to apps like DropBox and other generic file handling applications, what you really want is a UIDocumentInteractionController.

Use it something like this:

class ViewController {
    var openInController:UIDocumentInteractionController!

    init() {
        openInController = UIDocumentInteractionController(URL: docURL)
    }

    func shareDoc {
        openInController.presentOptionsMenuFromRect(CGRectZero, inView: self.view, animated: true)
    }
}
like image 2
voidref Avatar answered Oct 29 '22 10:10

voidref