1

I made an pdf creator app. After creating pdf file I want to display the created files on a tableview. I created Tableview Controller and DocumentDirectoryTableTableViewController. I don't know how to do it after all of that.

I found this and this questions but I couldn't implement it into my code.

Also better to add my code for saving file:

        // 5. Save PDF file
    let path = "/MyApp/PdfFiles/MyPDF.pdf"
    pdfData.write(toFile: path, atomically: true)
    print("open \(path)") // command to open the generated file

UPDATE: I can also get the App Directory and use it by this function:

    func getDocumentsDirectory() -> URL {
    let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
    let documentsDirectory = paths[0]
    return documentsDirectory
}

and

let path = "\(getDocumentsDirectory())MyApp.pdf"
Community
  • 1
  • 1
CactiApp
  • 89
  • 9
  • My problem is that I can't match these codes. I mean on I save here: `/MyApp/PdfFiles/MyPDF.pdf` but I have no idea how to implement this directory to the code. What's wrong with `print("open \(path)")`? I'm new developer please bear with me. @KrishnaCA – CactiApp Nov 30 '16 at 12:02
  • I just checked your updated code. At exactly what point are you stuck at? Also, you initial path "/MyApp/PdfFiles/MyPDF.pdf" is not correct. Because iOS file paths start differently. The way you are doing it in your updated code is correct – KrishnaCA Nov 30 '16 at 12:26
  • @KrishnaCA The problem is: I don't see any file on the tableview when I run the app. Even with the updated code I can't see any file. – CactiApp Nov 30 '16 at 12:34
  • Are you reading the file correctly? – KrishnaCA Nov 30 '16 at 13:44
  • @KrishnaCA Where should I implement the code that I have linked to DocumentDirectoryTableTableViewController.Swift exactly? I guess I have problem there. – CactiApp Nov 30 '16 at 13:48
  • I believe your reading part goes like this. 1) Display the name of files on UITableView 2) click on any file, display the entire pdf. Is this right? – KrishnaCA Nov 30 '16 at 14:05
  • @KrishnaCA yes I want to do it exactly – CactiApp Nov 30 '16 at 14:08
  • Do you also want to write some `NSData` to pdf file? – KrishnaCA Nov 30 '16 at 15:42
  • Hi, I wrote an answer. Let me know if it helps or not. If it doesn't, please let me know where you have doubt – KrishnaCA Nov 30 '16 at 16:54

1 Answers1

1

If you have NSData for the pdf file you're trying to store and if you want user to access it, you can write it using this:

func writePDF(data: NSData, to Name:String) -> Bool {

    let pdfData:NSData = data

    let pdfPaths:[String] = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
    var pdfPath = pdfPaths[0]
    pdfPath.append("_\(Name).pdf")
    let result:Bool = pdfData.write(toFile: pdfPath, atomically: true)

    return result
}

If you want to read pdf file programatically in your app given that you store it in the above path. It can be done using this:

func readPDF(Name: String) {

    let pdfPaths:[String] = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
    var pdfPath = pdfPaths[0]
    pdfPath.append("_\(Name).pdf")
    if let pdfData:NSData = NSData(contentsOfFile: pdfPath) { // verifying pdf exists in this path
        let documentController: UIDocumentInteractionController = UIDocumentInteractionController(url: URL(fileURLWithPath: pdfPath))
        documentController.delegate = self
        documentController.presentPreview(animated: true)
    }
}

// the below delegate function for `UIDocumentInteractionController` is necessary for it to present in the current `UIViewController`
func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController {
    return self
}
KrishnaCA
  • 5,615
  • 1
  • 21
  • 31