我将我的 pdfData 给用户保存。他可以将其保存为文件并制作一个文件,但是 pdf 文件的默认名称是:PDF document.pdf
。如果可能的话,我想要自己的文件名。也许我可以在将 pdfData 提供给 UIActivityViewController
之前更改 pdfData 中的文件名呢?
以下是我的代码:
// 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)
-- 更新 --
我的新想法是,首先尝试保存文件并尝试使用URL,如果失败,则直接使用pdfData,因为在某些模拟器中使用URL不会出现错误,在其他模拟器中则会出现错误。