【发布时间】:2019-12-31 01:00:46
【问题描述】:
我正在使用 PDFKit 创建一个 PDF 文件。
PDF 文档已正确创建,因为如果我尝试使用 PDFView 查看它,我会看到它(@IBAction func DetailButton) 可能不是……我没看到广场!
问题是我无法与 UIActivityViewController 共享它。 (@IBAction func ShareButton)
我共享一个文本文件,它是文件路径。 /var/mobile/Containers/Data/Application/....../Documents/1234.pdf
另外,我如何在分享到“邮件”时发送默认电子邮件地址,在分享到“消息”时发送电话号码?
func createPDFwithPDFKit(filePath:String, share:Bool) {
let pdfTitle = "Swift-Generated PDF"
let pdfMetadata = [
// The name of the application creating the PDF.
kCGPDFContextCreator: "XXX",
// The name of the PDF's author.
kCGPDFContextAuthor: "xxx",
// The title of the PDF.
kCGPDFContextTitle: game!.name,
// Encrypts the document with the value as the owner password. Used to enable/disable different permissions.
kCGPDFContextOwnerPassword: "myPassword123"
]
// Creates a new PDF file at the specified path.
UIGraphicsBeginPDFContextToFile(filePath, CGRect.zero, pdfMetadata)
// Creates a new page in the current PDF context.
UIGraphicsBeginPDFPage()
// Default size of the page is 612x72.
let pageSize = UIGraphicsGetPDFContextBounds().size
let font = UIFont.preferredFont(forTextStyle: .largeTitle)
// Let's draw the title of the PDF on top of the page.
let attributedPDFTitle = NSAttributedString(string: pdfTitle, attributes: [NSAttributedString.Key.font: font])
let stringSize = attributedPDFTitle.size()
let stringRect = CGRect(x: (pageSize.width / 2 - stringSize.width / 2), y: 20, width: stringSize.width, height: stringSize.height)
attributedPDFTitle.draw(in: stringRect)
// Closes the current PDF context and ends writing to the file.
UIGraphicsEndPDFContext()
if share {
let vc = UIActivityViewController(activityItems: [filePath], applicationActivities: [])
vc.excludedActivityTypes = [
UIActivity.ActivityType.assignToContact,
UIActivity.ActivityType.saveToCameraRoll,
UIActivity.ActivityType.postToFlickr,
UIActivity.ActivityType.postToVimeo,
UIActivity.ActivityType.postToTencentWeibo,
UIActivity.ActivityType.postToTwitter,
UIActivity.ActivityType.postToFacebook,
UIActivity.ActivityType.openInIBooks
]
present(vc, animated: true, completion: nil)
}
}
@IBAction func ShareButton(_ sender: UIBarButtonItem) {
alert.addAction(UIAlertAction(title: "Exporter au format PDF", style: .default, handler: { _ in
let fileName = "1234.pdf"
let documentsDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
let filePath = (documentsDirectory as NSString).appendingPathComponent(fileName) as String
self.createPDFwithPDFKit(filePath:filePath, share:true)
}))
alert.addAction(UIAlertAction.init(title: "Annuler", style: .cancel, handler: nil))
self.present(alert, animated: true, completion: nil)
}
@IBAction func DetailButton(_ sender: UIBarButtonItem) {
// Create and add a PDFView to the view hierarchy.
let documentsDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
let filePath = (documentsDirectory as NSString).appendingPathComponent("scores de '" + game!.name + "'.pdf") as String
self.createPDFwithPDFKit(filePath:filePath, share:false)
//View the PDF
let pdfView = PDFView(frame: view.bounds)
pdfView.autoScales = true
pdfView.displayMode = .singlePageContinuous
view.addSubview(pdfView)
// Create a PDFDocument object and set it as PDFView's document to load the document in that view.
let pdfDocument = PDFDocument(url: URL(fileURLWithPath: filePath))!
pdfView.document = pdfDocument
}
【问题讨论】:
标签: swift share uiactivityviewcontroller pdfkit