iosswiftcsvios-sharesheet

How to pass a Document File to UIActivityViewController i.e. Share Sheet


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.


Solution

  • 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
    }