iosswiftuidocumentinteraction

UIdocumentInteractionController cant display the PDF file in swift 4


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
}

above code i try on my apps, when click left menu , not able to show the pdf file. anyone got idea ?


Solution

  • I am showing here an example for download pdf from url link displaying in UIdocumentInteractionController in swift 4 as per your requirement

    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
    }
    

    }