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を開くことができます。
PDFをアプリケーションのDocumentsフォルダに保存できます。 'UIFileSharingEnabled'をYESに設定すると、ユーザーはiTunes経由でファイルにアクセスできます。 – frzi
作成したPDFファイルにiTunes経由でアクセスするにはどうすればよいですか?私は試しましたが見つかりませんでした。 @frzi – CactiApp
@CactiAppどこのファイルにアクセスしたいですか?アプリから、それとも他のドキュメントビューアプリですか? – Jeyamahesan