gpt4 book ai didi

ios - UIdocumentInteractionController 无法在 swift 4 中显示 PDF 文件

转载 作者:行者123 更新时间:2023-11-28 12:11:18 24 4
gpt4 key购买 nike

class ViewController: UIViewController,UIDocumentInteractionControllerDelegate
{
let documentInteractionController = UIDocumentInteractionController(url: URL(fileURLWithPath: path))
documentInteractionController.delegate = self
documentInteractionController.presentPreview(animated: true)
}

func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController
{
return self
}

上面的代码我在我的应用程序上尝试,当单击左侧菜单时,无法显示 pdf 文件。有人知道吗?

最佳答案

我在这里展示了一个示例,用于根据您的要求从 UIdocumentInteractionController 中显示在 swift 4 中的 url 链接下载 pdf

class ExampleViewController: UIViewController,  URLSessionDownloadDelegate, UIDocumentInteractionControllerDelegate {

let urlLink = "sample link";

override func viewDidLoad() {
super.viewDidLoad()
}


@IBAction func btnDownArrowOnPressed(_ sender: UIButton) {



if(urlLink != nil){
if(urlLink != nil && urlLink != "Null" && urlLink != ""){


let backgroundSessionConfiguration = URLSessionConfiguration.background(withIdentifier: "backgroundSession")
backgroundSession = Foundation.URLSession(configuration: backgroundSessionConfiguration, delegate: self, delegateQueue: OperationQueue.main)


let url = URL(string: urlLink!
downloadTask = backgroundSession.downloadTask(with: url)
downloadTask.resume()


}else{
self.view.makeToast("You don't have any certificate to download.")
}



}
else{
self.view.makeToast("You don't have any certificate to download.")
}


}


func showFileWithPath(path: String){
let isFileFound:Bool? = FileManager.default.fileExists(atPath: path)
if isFileFound == true{
let viewer = UIDocumentInteractionController(url: URL(fileURLWithPath: path))
viewer.delegate = self

viewer.presentPreview(animated: true)
}
}


func urlSession(_ session: URLSession,
downloadTask: URLSessionDownloadTask,
didFinishDownloadingTo location: URL){

let path = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)
let documentDirectoryPath:String = path[0]
let fileManager = FileManager()
let destinationURLForFile = URL(fileURLWithPath: documentDirectoryPath.appendingFormat("/Certificate.pdf"))

if fileManager.fileExists(atPath: destinationURLForFile.path){
showFileWithPath(path: destinationURLForFile.path)
}
else{
do {
try fileManager.moveItem(at: location, to: destinationURLForFile)
// show file
showFileWithPath(path: destinationURLForFile.path)
}catch{
print("An error occurred while moving file to destination url")
}
}
}
// 2
func urlSession(_ session: URLSession,
downloadTask: URLSessionDownloadTask,
didWriteData bytesWritten: Int64,
totalBytesWritten: Int64,
totalBytesExpectedToWrite: Int64){

}

//MARK: URLSessionTaskDelegate
func urlSession(_ session: URLSession,
task: URLSessionTask,
didCompleteWithError error: Error?){
downloadTask = nil

if (error != nil) {
print(error!.localizedDescription)
}else{
self.view.hideToastActivity()
print("The task finished transferring data successfully")
}
}

//MARK: UIDocumentInteractionControllerDelegate
func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController
{


UINavigationBar.appearance().tintColor = UIColor.white

return self
}

关于ios - UIdocumentInteractionController 无法在 swift 4 中显示 PDF 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48455366/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com