CactiApp
CactiApp

Reputation: 89

How to access created pdf document on iOS?

My app has an action that creates pdf file. After creating file on simulator I can access the created file on Mac but on iPhone I can not. Even I have searched but couldn't find. On Acrobat Reader neither.

So what can I do with this created pdf file? Should I convert it ePub or something?

This is my code for creating pdf:

func createPdfFromView(_ imageView: UIImageView, saveToDocumentsWithFileName fileName: String)
{
    let pdfData = NSMutableData()
    UIGraphicsBeginPDFContextToData(pdfData, imageView.bounds, nil)
    UIGraphicsBeginPDFPage()

    let pdfContext = UIGraphicsGetCurrentContext()

    if (pdfContext == nil)
    {
        return
    }

    imageView.layer.render(in: pdfContext!)
    UIGraphicsEndPDFContext()


}
@IBAction func createPdfAction(_ sender: UIButton) {

    createPdfFromView(ImageDisplay, saveToDocumentsWithFileName: "")
}

Upvotes: 0

Views: 956

Answers (1)

Jeyamahesan
Jeyamahesan

Reputation: 1111

Write the PDF Data as a file in local path

private var pdfData: NSMutableData!
private var filPath: NSString!
private var docController: UIDocumentInteractionController!

func writeDataAsFile()
{
    //Search for local path
    let paths = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)
    let directoryPath = paths[0] as NSString

    //Appending file name to be saved in directory path
    filPath = directoryPath.appendingPathComponent("TestPDF.pdf") as NSString!

    //Check file is already exist in path
    let isFileExists = FileManager.default.fileExists(atPath: filPath as String)

    if(!isFileExists)
    {
        //pdfData is NSData/NSMutableData, Write to the filepath
        pdfData.write(toFile: filPath as String, atomically: true)
    }
}

UIDocumentInteractionController provides in-app support for managing user interactions with files in the local system

@IBAction func goPDF(sender: UIButton) {

    docController = UIDocumentInteractionController.init(url: NSURL.fileURL(withPath: filPath as String, isDirectory: true))
    docController.delegate = self
    docController.presentOpenInMenu(from: CGRect.zero, in: self.view, animated: true)
}

UIDocumentInteractionControllerDelegate to handle document interactions

func documentInteractionController(_ controller: UIDocumentInteractionController, willBeginSendingToApplication application: String?) {
    print("willBeginSendingToApplication")
}

func documentInteractionController(_ controller: UIDocumentInteractionController, didEndSendingToApplication application: String?) {
    print("didEndSendingToApplication")
}

func documentInteractionControllerDidDismissOpenInMenu(_ controller: UIDocumentInteractionController) {
    print("documentInteractionControllerDidDismissOpenInMenu")
}

Using delegate in your ViewController should conforms the UIDocumentInteractionControllerDelegate like this, for example, in ViewController

import UIKit

class FirstViewController: UIViewController, UIDocumentInteractionControllerDelegate {
}

For open PDF in iBooks the menu will provide "Import with iBooks" option. This option will lead to open the PDF in iBooks after importing.

Upvotes: 1

Related Questions