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文件。有人有意见吗?
答案 0 :(得分:2)
我在这里显示了一个示例,可以根据您的要求从swift 4中的UIdocumentInteractionController中显示的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
}
}
答案 1 :(得分:0)
使用URL(string: path)
代替URL(fileURLWithPath: path)