简体   繁体   中英

UIDocumentInteractionController change file name for Sharing

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) 在此处输入图片说明

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)")
                }
            }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM