iOS Swift-如何显示adobe reader本地存储中的pdf文件
我正在尝试创建一个应用程序,其中pdf将保存在本地存储中,我希望该pdf在Adobe Reader中打开 另外,我不想在任何web视图中打开pdf。事实上,我希望pdf能在AdobeReader中打开 我搜索了很多,但是没有成功,所有的例子都是在web视图中打开pdf 下面是我的代码:iOS Swift-如何显示adobe reader本地存储中的pdf文件,ios,iphone,swift,xcode,pdf,Ios,Iphone,Swift,Xcode,Pdf,我正在尝试创建一个应用程序,其中pdf将保存在本地存储中,我希望该pdf在Adobe Reader中打开 另外,我不想在任何web视图中打开pdf。事实上,我希望pdf能在AdobeReader中打开 我搜索了很多,但是没有成功,所有的例子都是在web视图中打开pdf 下面是我的代码: @IBAction func savePDF(_ sender: Any) { let pdfData = Data(base64Encoded: FilePDF, options: .ignor
@IBAction func savePDF(_ sender: Any) {
let pdfData = Data(base64Encoded: FilePDF, options: .ignoreUnknownCharacters)
let array = pdfData?.withUnsafeBytes
{
[UInt8](UnsafeBufferPointer(start: $0, count: (pdfData?.count)!))
}
let data = NSData(bytes: array, length: (array?.count)!);
let tmpDir = NSTemporaryDirectory()
// data.write(toFile: tmpDir.appending("HandyHR_File_YEAR.pdf"), atomically: true)
print(array!)
print(data)
print(tmpDir)
data.write(toFile: tmpDir.appending("HandyHR_File_YEAR.pdf"), atomically: true)
}
变量tmpDir将pdf的路径存储在本地存储器中
任何帮助都将不胜感激
提前感谢。您可以使用QuickLook框架来显示pdf、word等文件。 此处一个选项用于显示文件: 包含控制器的实例变量:
fileprivate lazy var docInteractionController: UIDocumentInteractionController = {
return UIDocumentInteractionController()
}()
演示预览:
let tmpDir = NSTemporaryDirectory()
let fileName = "HandyHR_File_YEAR.pdf"
// Save file
let url = URL(fileURLWithPath: tmpDir).appendingPathComponent(fileName)
docInteractionController.url = url
docInteractionController.delegate = self
if docInteractionController.presentPreview(animated: true) {
// Successfully displayed
} else {
// Couldn't display
}
extension YourViewController: UIDocumentInteractionControllerDelegate {
func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController {
// I tend to use 'navigationController ?? self' here but depends on implementation
return self
}
}
还可以实现委托以指定显示预览的视图控制器:
let tmpDir = NSTemporaryDirectory()
let fileName = "HandyHR_File_YEAR.pdf"
// Save file
let url = URL(fileURLWithPath: tmpDir).appendingPathComponent(fileName)
docInteractionController.url = url
docInteractionController.delegate = self
if docInteractionController.presentPreview(animated: true) {
// Successfully displayed
} else {
// Couldn't display
}
extension YourViewController: UIDocumentInteractionControllerDelegate {
func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController {
// I tend to use 'navigationController ?? self' here but depends on implementation
return self
}
}
您可能需要显示UIDocumentInteractionController并让用户选择“查看”。@Tj3n如果我不希望用户在web视图中查看文件,该怎么办。是的,如果用户想要打开文件pdf查看器而不是Adobe,我可以为用户提供选项。但我只得到了web视图的示例。为什么不使用QLPreviewController或UIDocumentInteractionController呢?查看我之前的评论以供参考。@AnuragSharma我已经在上面提到了我不想使用Web视图。