|
@@ -3,13 +3,16 @@ import UIKit
|
|
|
import ComPDFKit
|
|
|
import ComPDFKit_Tools
|
|
|
|
|
|
-public class CompdfkitFlutterPlugin: NSObject, FlutterPlugin, CPDFViewBaseControllerDelete {
|
|
|
+public class CompdfkitFlutterPlugin: NSObject, FlutterPlugin, CPDFViewBaseControllerDelete, UIDocumentPickerDelegate {
|
|
|
|
|
|
public var messager : FlutterBinaryMessenger?
|
|
|
-
|
|
|
+
|
|
|
+ private var startAccessing: Bool = false
|
|
|
+
|
|
|
+ private var _reuslt: FlutterResult?
|
|
|
|
|
|
public static func register(with registrar: FlutterPluginRegistrar) {
|
|
|
- let channel = FlutterMethodChannel(name: "com.compdfkit.flutter.plugin", binaryMessenger: registrar.messenger())
|
|
|
+ let channel = FlutterMethodChannel.init(name: "com.compdfkit.flutter.plugin", binaryMessenger: registrar.messenger())
|
|
|
|
|
|
let instance = CompdfkitFlutterPlugin()
|
|
|
instance.messager = registrar.messenger()
|
|
@@ -43,17 +46,29 @@ public class CompdfkitFlutterPlugin: NSObject, FlutterPlugin, CPDFViewBaseContro
|
|
|
let path = initInfo?["document"] as? String ?? ""
|
|
|
let document = NSURL(fileURLWithPath: path)
|
|
|
|
|
|
- let fileManager = FileManager.default
|
|
|
- let samplesFilePath = NSHomeDirectory().appending("/Documents/Files")
|
|
|
- let fileName = document.lastPathComponent ?? ""
|
|
|
- let docsFilePath = samplesFilePath + "/" + fileName
|
|
|
+ var success = false
|
|
|
+ var documentPath = path
|
|
|
|
|
|
- if !fileManager.fileExists(atPath: samplesFilePath) {
|
|
|
- try? FileManager.default.createDirectory(atPath: samplesFilePath, withIntermediateDirectories: true, attributes: nil)
|
|
|
+ if startAccessing {
|
|
|
+ success = document.startAccessingSecurityScopedResource()
|
|
|
+
|
|
|
+ startAccessing = false
|
|
|
+ } else {
|
|
|
+ let fileManager = FileManager.default
|
|
|
+ let samplesFilePath = NSHomeDirectory().appending("/Documents/Files")
|
|
|
+ let fileName = document.lastPathComponent ?? ""
|
|
|
+ let docsFilePath = samplesFilePath + "/" + fileName
|
|
|
+
|
|
|
+ if !fileManager.fileExists(atPath: samplesFilePath) {
|
|
|
+ try? FileManager.default.createDirectory(atPath: samplesFilePath, withIntermediateDirectories: true, attributes: nil)
|
|
|
+ }
|
|
|
+
|
|
|
+ try? FileManager.default.copyItem(atPath: document.path ?? "", toPath: docsFilePath)
|
|
|
+
|
|
|
+ documentPath = docsFilePath
|
|
|
}
|
|
|
|
|
|
- try? FileManager.default.copyItem(atPath: document.path ?? "", toPath: docsFilePath)
|
|
|
-
|
|
|
+
|
|
|
let jsonDataParse = CPDFJSONDataParse(String: jsonString as! String)
|
|
|
guard let configuration = jsonDataParse.configuration else { return }
|
|
|
if let rootViewControl = UIApplication.shared.keyWindow?.rootViewController {
|
|
@@ -63,15 +78,26 @@ public class CompdfkitFlutterPlugin: NSObject, FlutterPlugin, CPDFViewBaseContro
|
|
|
tRootViewControl = presentedViewController
|
|
|
}
|
|
|
|
|
|
- let pdfViewController = CPDFViewController(filePath: docsFilePath, password: nil, configuration: configuration)
|
|
|
+ let pdfViewController = CPDFViewController(filePath: documentPath, password: nil, configuration: configuration)
|
|
|
let navController = CNavigationController(rootViewController: pdfViewController)
|
|
|
pdfViewController.delegate = self
|
|
|
navController.modalPresentationStyle = .fullScreen
|
|
|
tRootViewControl.present(navController, animated: true)
|
|
|
}
|
|
|
+
|
|
|
+ if success {
|
|
|
+ document.stopAccessingSecurityScopedResource()
|
|
|
+ }
|
|
|
case "get_temporary_directory":
|
|
|
result(self.getTemporaryDirectory())
|
|
|
|
|
|
+ case "get_pdf_file_path":
|
|
|
+ let documentTypes = ["com.adobe.pdf"]
|
|
|
+ let documentPickerViewController = UIDocumentPickerViewController(documentTypes: documentTypes, in: .open)
|
|
|
+ documentPickerViewController.delegate = self
|
|
|
+ UIApplication.presentedViewController()?.present(documentPickerViewController, animated: true, completion: nil)
|
|
|
+ _reuslt = result
|
|
|
+
|
|
|
default:
|
|
|
result(FlutterMethodNotImplemented)
|
|
|
}
|
|
@@ -88,4 +114,15 @@ public class CompdfkitFlutterPlugin: NSObject, FlutterPlugin, CPDFViewBaseContro
|
|
|
baseControllerDelete.dismiss(animated: true)
|
|
|
}
|
|
|
|
|
|
+ // MARK: - UIDocumentPickerDelegate
|
|
|
+
|
|
|
+ public func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
|
|
|
+ let fileUrlAuthozied = urls.first?.startAccessingSecurityScopedResource() ?? false
|
|
|
+ if fileUrlAuthozied {
|
|
|
+ let filePath = urls.first?.path ?? ""
|
|
|
+ _reuslt?(filePath)
|
|
|
+ startAccessing = true
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
}
|