简体   繁体   English

UIDocumentInteractionController 更改共享文件名

[英]UIDocumentInteractionController change file name for Sharing

iam using the following code to show the sharing options for PDF我使用以下代码显示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,问题是我用日期戳保存了 PDF 文件名,以避免有两个同名的文件,但是当显示共享选项时,实际文件名出现了,

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)在这种情况下,我们可以创建一个临时文件夹,该文件夹可以包含与 lastPathExtension 相同的文件,即 document.fileExtension,我们可以将这个新文件路径传递给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.并且 UIDocumentInteractionController 被解除,删除documentInteractionControllerDidEndPreview(_ controller: UIDocumentInteractionController)方法上的临时documentInteractionControllerDidEndPreview(_ controller: UIDocumentInteractionController)

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Swift 4共享音频文件(UIDocumentInteractionController) - Swift 4 sharing audio file (UIDocumentInteractionController) 在另一个应用程序中打开文件时,在UIDocumentInteractionController中更改文件名 - Change file name in UIDocumentInteractionController when opening file in another app iOS UIDocumentInteractionController PDF共享问题 - iOS UIDocumentInteractionController PDF sharing issue 使用 UIActivityViewController/UIDocumentInteractionController 共享文件不起作用 - [无法通过 AirDrop、Whatapp、iMessage、Mail 等共享] - File sharing using UIActivityViewController/ UIDocumentInteractionController not working - [can't share via AirDrop, Whatapp, iMessage, Mail, etc.] 更改UIDocumentInteractionController上的NavigationBar背景 - Change NavigationBar background on UIDocumentInteractionController 通过UIDocumentInteractionController打开文件 - Opening a file by UIDocumentInteractionController 无法在UIDocumentInteractionController中打开PDF文件 - PDF file not open in UIDocumentInteractionController 来自web的UIDocumentInteractionController和文件 - UIDocumentInteractionController and file from web UIDocumentInteractionController,没有文件扩展名但是UTI - UIDocumentInteractionController, No File Extension but UTI 使用UIActivityViewController的WhatsApp不使用UIDocumentInteractionController来共享图像 - WhatsApp with UIActivityViewController for sharing image not using UIDocumentInteractionController
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM