0

iam using the following code to show the sharing options for PDF

    self.documentController = UIDocumentInteractionController(url: url)
    self.documentController.name = "Test name" // not working
    self.documentController.presentOptionsMenu(from: self.shareButton, animated: true)

the problem is that I save the PDF file name with datestamp to avoid having two files with the same name, but when the share options is being shown the actual file name appears,

is there is a way to show custom name instead of the actual filename (I don't want to copy the file to other place and rename it, waste of time and performance)enter image description here

Hazem Badr
  • 41
  • 4

1 Answers1

0

In such a situation, we can create a temporary folder which can contain the same file with lastPathExtension will be document.fileExtension and we can pass this newly file path to UIDocumentInteractionController.init(url: newFileUrl)

For Example:

func openUnsupportedFileWithPath(documentName : String, fileurl : URL, fileExtension : String, aDocument: SILDocumentDB? = nil, sourceView: UIView? = nil) -> Void {

    // Create new temporary path
    let paths: String = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
    var newFileUrl: String = paths.appending("/Downloads/TemporaryFolder)")
    newFileUrl = newFileUrl.appendingFormat("%@","\(documentName)")

    let destinationPathUrl : URL
    do {
        // Move newly filePath with new fileName and fileExtension
        destinationPathUrl = URL(fileURLWithPath: destinanewFileUrltionPath)
        try FileManager.default.moveItem(at: fileurl, to: destinationPathUrl)
    } catch {
        print(error)
    }

    //Pass newly filePath to UIDocumentInteractionController
    documentInteractionController = UIDocumentInteractionController.init(url: newFileUrl)
    documentInteractionController?.name = documentName
    documentInteractionController?.delegate = self

    let canPreview = documentInteractionController?.presentPreview(animated: true)
    if (canPreview == false) {
        let activityViewController = UIActivityViewController.init(activityItems: [fileurl], applicationActivities: nil)

        activityViewController.setValue(documentName, forKey: "subject")
        if ISIPAD {
            activityViewController.popoverPresentationController?.sourceView = sourceView ?? self.view
        }
        self.present(activityViewController, animated: true, completion: nil)
    } 
}

And UIDocumentInteractionController get dismiss, remove the temporary filePath on documentInteractionControllerDidEndPreview(_ controller: UIDocumentInteractionController) method.

public func documentInteractionControllerDidEndPreview(_ controller: UIDocumentInteractionController) {
        documentInteractionController = nil
        let paths: String = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
        let filePath: String = paths.appending("/Downloads/TemporaryFolder)")

        let _fileManager : FileManager  = FileManager.default
        if filePath.length > 0 {
            if _fileManager.fileExists(atPath: filePath) {
                do{
                    try _fileManager.removeItem(atPath: filePath)
                }catch  let error as NSError{

                    print("\(error.localizedDescription)")
                }
            }
        }
    }
Bhanu
  • 1,249
  • 10
  • 17