如何将文档文件传递给 UIActivityViewController 即 Share Sheet

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

我有一个 iOS 应用程序,它生成一个 CSV 文件并将其保存到设备的工作文档目录中。现在,当用户按下按钮时,会显示 UIActivityViewController 共享 Sheet,允许您打开向其他应用程序发送数据。

我的问题是如何将此 CSV 文件传递​​给共享 Sheet。我知道如何使它与文本和图像一起工作,但不确定如何使它与 CSV 文件一起工作。最终结果是我希望此文件在从共享 Sheet.

中选择的任何电子邮件客户端中显示为附件

这是我用来打开文档(word、pdf 等)的代码。应该适合你...

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