Ios 有没有一种方法可以获取从文档目录生成并保存到ibooks或电子邮件的pdf?
我正试图打开一个由Ios 有没有一种方法可以获取从文档目录生成并保存到ibooks或电子邮件的pdf?,ios,swift,Ios,Swift,我正试图打开一个由UItableView生成的pdf文件,并将其显示在iBooks中 我使用了以下代码 extension UITableView { // Export pdf from UITableView and save pdf in drectory and return pdf file path func exportAsPdfFromTable() -> String { let originalBounds = self.bounds
UItableView
生成的pdf文件,并将其显示在iBooks中
我使用了以下代码
extension UITableView {
// Export pdf from UITableView and save pdf in drectory and return pdf file path
func exportAsPdfFromTable() -> String {
let originalBounds = self.bounds
self.bounds = CGRect(x:originalBounds.origin.x, y: originalBounds.origin.y, width: self.contentSize.width, height: self.contentSize.height)
let pdfPageFrame = CGRect(x: 0, y: 0, width: self.bounds.size.width, height: self.contentSize.height)
let pdfData = NSMutableData()
UIGraphicsBeginPDFContextToData(pdfData, pdfPageFrame, nil)
UIGraphicsBeginPDFPageWithInfo(pdfPageFrame, nil)
guard let pdfContext = UIGraphicsGetCurrentContext() else { return "" }
self.layer.render(in: pdfContext)
UIGraphicsEndPDFContext()
self.bounds = originalBounds
// Save pdf data
return self.saveTablePdf(data: pdfData)
}
// Save pdf file in document directory
func saveTablePdf(data: NSMutableData) -> String {
let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
let docDirectoryPath = paths[0]
let pdfPath = docDirectoryPath.appendingPathComponent("tablePdf.pdf")
if data.write(to: pdfPath, atomically: true) {
return pdfPath.path
} else {
return ""
}
}
}
然后我从一个带有iAction的按钮调用该函数,并尝试使用文档目录的文件路径,但它不起作用
@IBAction func export() {
let pdfFilePath = self.tableView.exportAsPdfFromTable()
print(pdfFilePath)
if let url = URL(string:"itms-books://var/mobile/Containers/Data/Application/3FF01C35-270E-49A0-88DD-E88175A93FCC/Documents/tablePdf.pdf") {
if UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, options: [:])
}
}
}
pdf文件保存在simulators文档目录中,是否有方法将其保存在平板电脑中?在ibooks中打开pdf的最佳方式是什么?以下是一个示例
import UIKit
class ViewController: UIViewController {
private static let filename = "myfile.pdf"
private var documentInteractionController: UIDocumentInteractionController?
override func viewDidLoad() {
super.viewDidLoad()
let pdfData = UIGraphicsPDFRenderer(bounds: .init(origin: .zero, size: .init(width: 400, height: 400))).pdfData { context in
context.beginPage()
let attributes = [
NSAttributedString.Key.font : UIFont.boldSystemFont(ofSize: 150)
]
let text = "Hello!" as NSString
text.draw(in: CGRect(x: 0, y: 0, width: 500, height: 200), withAttributes: attributes)
}
if let url = try? FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false) {
let pdfURL = url.appendingPathComponent(ViewController.filename)
try? pdfData.write(to: pdfURL)
}
}
@IBAction func tapShareButton(_ sender: UIButton) {
if let url = try? FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false) {
let pdfURL = url.appendingPathComponent(ViewController.filename)
documentInteractionController = UIDocumentInteractionController(url: pdfURL)
documentInteractionController?.presentOpenInMenu(from: sender.frame, in: view, animated: true)
}
}
}
我没有使用文档目录中的pdf,而是通过使用
UIActivityViewController
来存储可以复制的信息来传递数据
let activityViewController : UIActivityViewController
if let personaImage = self.personas[indexPath.row].image, let imageToShare = UIImage(data: personaImage as Data){
//in swift if lets are used to se if an opional has a value or not
activityViewController = UIActivityViewController(activityItems: ["With personia I am sharing my persona named " + self.personas[indexPath.row].name!, imageToShare, "Project name: " + self.personas[indexPath.row].projectName!, "Age: " + self.personas[indexPath.row].age!, "Location: " + self.personas[indexPath.row].location!, "Gender: " + self.personas[indexPath.row].gender!, "Disabilities: " + self.personas[indexPath.row].disability!, "Occupation: " + self.personas[indexPath.row].occupation!, "Bio: " + self.personas[indexPath.row].bio!, "goals: " + self.personas[indexPath.row].goals!, "Motivation One: " + self.personas[indexPath.row].motivation!, "Motivation Two: " + self.personas[indexPath.row].motivationTwo!, "Motivation Three: " + self.personas[indexPath.row].motivationThree!, "Frustration One: " + self.personas[indexPath.row].frustration!, "Frustration Two: " + self.personas[indexPath.row].frustrationTwo!, "Frustration Three: " + self.personas[indexPath.row].frustrationThree!], applicationActivities: nil)
} else {
activityViewController = UIActivityViewController(activityItems: ["With personia I am sharing my persona named " + self.personas[indexPath.row].name!], applicationActivities: nil)
}
self.present(activityViewController, animated: true, completion: nil)
if let popOver = activityViewController.popoverPresentationController {
popOver.sourceView = self.view
}
}
为什么不使用
UIActivityViewController
并让用户选择如何处理PDF?我该怎么做呢,我正在iPad上制作这个应用程序,很抱歉,Xcode还是新手,在UIActivityViewController
上搜索一下,你会发现很多示例和教程。谢谢@rmaddy的关注,如果将UIActivityViewController
保存在simulators文档目录中,使用它是否仍然有效?只需将PDF的URL传递到UIActivityViewController
。