2016-10-27 13 views
0

私のアプリには、pdfファイルを作成するアクションがあります。シミュレータ上でファイルを作成した後、Mac上で作成したファイルにアクセスできますが、iPhone上ではアクセスできません。私は検索しても見つけられませんでした。 Acrobat ReaderではなくiOSで作成したPDF文書にアクセスするには?

このように作成されたpdfファイルで何ができますか?私はそれをePubか何かに変換すべきですか?

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: "") 
} 
+0

PDFをアプリケーションのDocumentsフォルダに保存できます。 'UIFileSharingEnabled'をYESに設定すると、ユーザーはiTunes経由でファイルにアクセスできます。 – frzi

+0

作成したPDFファイルにiTunes経由でアクセスするにはどうすればよいですか?私は試しましたが見つかりませんでした。 @frzi – CactiApp

+0

@CactiAppどこのファイルにアクセスしたいですか?アプリから、それとも他のドキュメントビューアプリですか? – Jeyamahesan

答えて

1

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は内のファイルとユーザーとの対話を管理するためのアプリのサポートを提供しますローカルパスにファイルとしてPDFデータを書き込む:

これは、PDFファイルを作成するための私のコードですローカルシステム

@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ドキュメントinteraを処理するあなたのViewControllerにデリゲートを使用してctions

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

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

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

メニューは、 "インポートのiBooksと" オプションを提供しますiBooksの中で開いてPDFについてのViewController

import UIKit 

class FirstViewController: UIViewController, UIDocumentInteractionControllerDelegate { 
} 

で、例えば、このようなUIDocumentInteractionControllerDelegateに準拠する必要があります。このオプションを選択すると、インポート後にiBooksでPDFを開くことができます。

+0

私はiOSプログラミングの新機能です。私と一緒に抱きしめてください。 'docController.delegate = self'にエラーがあります。エラーは* UIDocumentInteractionControllerDelagete型にViewController型の値を代入できません。 – CactiApp

+0

は、クラスがUIDocumentInteractionControllerDelegateに準拠していることを宣言します。このように、クラスFirstViewController:UIViewController、UIDocumentInteractionControllerDelegate – Jeyamahesan

+0

@CactiApp更新された回答を確認 – Jeyamahesan

関連する問題