rachna sharma
rachna sharma

Reputation: 111

iOS Swift - How to show a pdf file, which is inside local storage in the adobe reader

I am trying to create an application where a the pdf is getting saved in the local storage and I want that pdf to be opened in Adobe Reader.

Also, I don't want to open the pdf inside any web view. Infact, I want pdf to be opened in Adobe Reader.

I have searched a lot but had no success, all the examples were for opening pdf inside web view.

Below is my code:

@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)

   }

The variable tmpDir stores the path for the pdf in the local storage.

Any help would b appreciated.

Thanks in advance.

Upvotes: 0

Views: 1775

Answers (2)

Yasir
Yasir

Reputation: 2372

One option is to use UIDocumentInteractionController to display the file:

Instance variable containing the controller:

fileprivate lazy var docInteractionController: UIDocumentInteractionController = {
    return UIDocumentInteractionController()
}()

Presenting the preview:

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
}

You would also implement the delegate to specify the view controller which presents the preview:

extension YourViewController: UIDocumentInteractionControllerDelegate {
    func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController {
        // I tend to use 'navigationController ?? self' here but depends on implementation
        return self 
    }
}

Upvotes: 1

Halil İbrahim YILMAZ
Halil İbrahim YILMAZ

Reputation: 740

You can use QuickLook framework for show pdf, word, etc. file. link here

Upvotes: 0

Related Questions