how to give pdfData a filename for user to save swift

╄→гoц情女王★ 提交于 2020-01-15 07:07:49

问题


I give my pdfData to user to save. He can save to files and make a file, but the default name of the pdf file is: PDF document.pdf. I want my own filename if this is possible. Perhaps I can change the filename within the pdfData before I give pdfData to UIActivityViewController?

Here is my code

    // Create page rect
    let pageRect = CGRect(x: 0, y: 0, width: 595.28, height: 841.89) // A4, 72 dpi

    // Create PDF context and draw
    let pdfData = NSMutableData()

    UIGraphicsBeginPDFContextToData(pdfData, pageRect, nil)
    UIGraphicsBeginPDFPage()

    // From here you can draw page, best make it in a function
    PdfErstellung.PdfErstellen(auswahlZeilen, vitalstoffWerteListe, heuteString)

    UIGraphicsEndPDFContext()

    // Save pdf DATA through user
    let activityViewController = UIActivityViewController(activityItems: [pdfData], applicationActivities: nil)
    activityViewController.popoverPresentationController?.sourceView = self.view // für IPAD nötig
    self.present(activityViewController, animated: true, completion: nil)

-- UPDATE --

My new idea ist, first try save the file and try a URL, and if this fail, then use the pdfData directly, because in some simulator use URL give no error and in other give error.

More here: https://stackoverflow.com/a/52499637/10392572


回答1:


You just need to save your pdfData to a temporary fileURL and share that URL.

let temporaryFolder = FileManager.default.temporaryDirectory
let fileName = "document.pdf"
let temporaryFileURL = temporaryFolder.appendingPathComponent(fileName)
print(temporaryFileURL.path)  // /Users/lsd/Library/Developer/XCPGDevices/E2003834-07AB-4833-B206-843DC0A52967/data/Containers/Data/Application/322D1F1D-4C97-474C-9040-FE5E740D38CF/tmp/document.pdf
do {
    try pdfData.write(to: temporaryFileURL)
    // your code
    let activityViewController = UIActivityViewController(activityItems: [temporaryFileURL], applicationActivities: nil)
} catch {
    print(error)
}



回答2:


Note that you can also set a user title for AirDrop to be used as follows:

    let temporaryFolder = FileManager.default.temporaryDirectory
    let fileName = "Carburant Discount Historique des Prix au \(Date()).json"
    let temporaryFileURL = temporaryFolder.appendingPathComponent(fileName)
    print(temporaryFileURL.path)
    do {
        try? FileManager.default.removeItem(at: temporaryFileURL)

        try json.write(to: temporaryFileURL)
    }
    catch let error {
        print("\(#function): *** Error while writing json to temporary file. \(error.localizedDescription)")
        alert("Export impossible")
        return
    }

    /// Sets the **title** along with the URL
    let dataToShare: [Any] = ["Historique des prix", temporaryFileURL]

    let activityViewController = UIActivityViewController(activityItems: dataToShare, applicationActivities: nil)


来源:https://stackoverflow.com/questions/52467356/how-to-give-pdfdata-a-filename-for-user-to-save-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!