Sharing a file using UIDocumentInteractionController

Hi


I'm trying to share a PDF file stored on the phone using the UIDocumentInteractionController

But even though the menu appears with all the options none of them work. I think I was able to provide a bit of more information when I tried to use the AirDrop. I got a debug message saying "The transferer faild because there are no valid files to send" (this is a rough translation, the message was localized)


I have the following code


extension ExportedReportsTableViewController: UIDocumentInteractionControllerDelegate {
  
    func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController {
        return self
    }
  
    func shareFile(filePath: String, loc: CGRect) {
      
        let stripPath = filePath.components(separatedBy: "/")
        let fileName = stripPath[stripPath.count - 1]
        let timestamp = fileName.replacingOccurrences(of: "Report", with: "").replacingOccurrences(of: ".pdf", with: "")
      
        let newFileName = Date(ticks: Double(timestamp)!).toString(format: "dd MMMM yyyy HH:mm")
      
        let nsURL = NSURL.fileURL(withPath: filePath)
        let url = nsURL as URL
        let dic = UIDocumentInteractionController(url: url.absoluteURL)
        dic.uti = "com.adobe.pdf"
        dic.delegate = self
        dic.name = newFileName
        //dic.presentPreview(animated: true) <-- It works perfectly
        dic.presentOptionsMenu(from: loc, in: self.view, animated: true)
      
    }
}


If I use the (presentPreview) method it works perfectly (even the share inside that controller) but with the presentOptionsMenu method it fails to send (the menu appears)


I can only assume that's a function for the delegate? The url is valid (it works on the preview)


Any ideas?


BTW it's possible, when sending change the file name?

For example the file name is "Report082182414.pdf" and when sent it appears "17 July 2017.pdf" (I know a work arround but requires copying the file with a new name and I wanted to avoid that)


Thanks in advance

Pedro Cavaleiro

As I understood, you are not able to see file attached in document interaction controller. This might be an issue because of ARC, try declaring UIDocumentInteractionController variable as var instead of let outside the function. Hope this helps.

Did you find a solution for your problem? I am experiencing the same issue in iOS 11. UIDocumentInteractionController does display the popover with the options. However, when trying to open the file in the selected application nothing happens. The file is not being sent to the selected application. However, I am able to use AirDrop, save it to the files app, attach it to an email, ... .

Thanks! It helped me with the same problem 🙂

Dude I struggled for hours trying to figure out this thing and finally I saw your answer here, thanks a lot!

Still the same issue with iOS 12. Using var did not help. As far as I understand the code, let should be the correct choice anyway.
For reference, here is my code (formatting is not really working in this forum):

@IBAction func exportFileButton(_ sender: Any) {
     let tmpDirURL = FileManager.default.temporaryDirectory
     let filename = Date().toJS()+".csv"
     let url = tmpDirURL.appendingPathComponent(filename
     URLSession.shared.dataTask(with: url) { data, response, error in
          do {            
               try Model.shared.exportCSV().write(to: url, atomically: true, encoding: .utf8)     
          } catch {
               self.alert(title: "Failed", message: "Export not working: +\(error)")
          }
       DispatchQueue.main.async {
       let documentController = UIDocumentInteractionController(url: url)
       documentController.delegate = self
       documentController.uti = "public.comma-separated-values-text"
       documentController.name = filename
       documentController.presentPreview(animated: true) //this works
       //documentController.presentOptionsMenu(from: (sender as AnyObject).frame, in:self.view, animated:true) //this does not work
     }
  }.resume()
}
Sharing a file using UIDocumentInteractionController
 
 
Q