1

I have an iOS App which generates a CSV File and saves it to the working Documents directory of the device. Now when a user presses a button, the UIActivityViewController Share Sheet is displayed which allows you to open send data to other apps.

My question is how do I pass this CSV file to the Share Sheet. I know how to make this work with Text and Images, but not exactly sure how to get this to work with a CSV file. The end result is that I want this file to show up as an attachment in any email client which is selected from the Share Sheet.

Jonas Deichelmann
  • 3,513
  • 1
  • 30
  • 45
Lavvo
  • 1,024
  • 3
  • 16
  • 35

1 Answers1

0

This is the code I use to open a document (word, pdf, etc.). Should work for you...

@IBAction func openDocument(sender: AnyObject)
{
    let interactionController = UIDocumentInteractionController(URL: documentURL)

    // (build your document's URL from its path in the Documents directory)

    interactionController.delegate = self


    // First, attempt to show the "Open with... (app)" menu. Will fail and
    // do nothing if no app is present that can open the specified document
    // type.

    let result = interactionController.presentOpenInMenuFromRect(
        self.view.frame,
        inView: self.view,
        animated: true
    )


    if result == false {

        // Fall back to "options" view:

        interactionController.presentOptionsMenuFromRect(
            self.view.frame,
            inView: self.view,
            animated: true)
    }

    // DONE
}
Nicolas Miari
  • 16,006
  • 8
  • 81
  • 189
  • Super Cool, thx bud. This works, except for one thing, it seems to work with some programs and not others. Why is that? For example, when I select GMail, the GMail Client and composer opens up, but the file is not attached to it. On the other hand, when I select one of my File Transfer programs, it works in that case. – Lavvo Sep 07 '15 at 04:51
  • 1
    and another thing, why is the Apple Mail Client not showing in the Document Selection list? – Lavvo Sep 07 '15 at 04:52
  • I don't think there's much you can configure; the system pretty much determines the sheet's contents based on file type and installed apps, as far as I know. – Nicolas Miari Sep 07 '15 at 04:54
  • 1
    By the way, the code I posted is basically Apple's sample code. – Nicolas Miari Sep 07 '15 at 04:55
  • 1
    I think you may be right, I was just looking at my log files in the console and it shows the right extensions were called etc...but the plugin invalidated the contents. So I guess there's not much I can do. In any case, I will still accept your answer since it's technically correct, but more so out of my control due to Apple's API I guess. – Lavvo Sep 07 '15 at 05:01
  • @Lavvo `UIActivityViewController(activityItems: [fileURL], applicationActivities: nil)` Refer: https://stackoverflow.com/questions/35851118/how-do-i-share-files-using-share-sheet-in-ios – user1046037 Apr 06 '18 at 06:19