2016-11-30 13 views
1

pdf作成アプリを作った。 pdfファイルを作成した後、作成したファイルをtableviewに表示します。私はTableview ControllerとDocumentDirectoryTableTableViewControllerを作成しました。私はそれの後にそれをする方法を知らない。TableViewで保存したファイルを表示して開く

thisthisが見つかりましたが、私のコードに実装できませんでした。 また

より良い

は、ファイルを保存するための私のコードを追加します。

 // 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: 私もアプリのディレクトリを取得し、この機能によって、それを使用することができます。

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

let path = "\(getDocumentsDirectory())MyApp.pdf" 
+0

私の問題は、私はこれらのコードに一致することはできません。私はここで '/ MyApp/PdfFiles/MyPDF.pdf'を保存することを意味しますが、このディレクトリをコードに実装する方法はわかりません。 'print(" open \(path) ")'の何が問題なのですか?私は新しい開発者です。 @KrishnaCA – CactiApp

+0

あなたの更新されたコードをチェックしました。あなたはどこにいるのですか?また、初期パス "/MyApp/PdfFiles/MyPDF.pdf"が正しくありません。 iOSファイルのパスは異なる方法で開始されるためです。あなたの更新されたコードでそれをやっている方法は正しいです – KrishnaCA

+0

@ KrishnaCA問題は:私は、アプリケーションを実行すると、tableview上のファイルが表示されません。更新されたコードでも、私はファイルを見ることができません。 – CactiApp

答えて

1

あなたが保存するために、あなたがそれにアクセスするためのユーザーをしたい場合は、あなたがこれを使用して、それを書くことができますしようとしているPDFファイルの:

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 
} 

あなたはそれを保存することを考えれば、あなたのアプリケーションでプログラム的にPDFファイルを読みたい場合は上記のパスで。これを使用して行うことができます:

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 
} 
+0

私は 'readPDF'関数を試しています。関数を呼び出すと、 'Name:String'のために何を書くべきですか? – CactiApp

+0

あなたのpdfを保存しましたか?つまりどこかに書きましたか? – KrishnaCA

関連する問題