CompdfkitFlutterPlugin.swift 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import Flutter
  2. import UIKit
  3. import ComPDFKit
  4. import ComPDFKit_Tools
  5. public class CompdfkitFlutterPlugin: NSObject, FlutterPlugin, CPDFViewBaseControllerDelete, UIDocumentPickerDelegate {
  6. public var messager : FlutterBinaryMessenger?
  7. private var startAccessing: Bool = false
  8. private var _reuslt: FlutterResult?
  9. public static func register(with registrar: FlutterPluginRegistrar) {
  10. let channel = FlutterMethodChannel.init(name: "com.compdfkit.flutter.plugin", binaryMessenger: registrar.messenger())
  11. let instance = CompdfkitFlutterPlugin()
  12. instance.messager = registrar.messenger()
  13. registrar.addMethodCallDelegate(instance, channel: channel)
  14. let factory = CPDFViewCtrlFactory(messenger: registrar.messenger())
  15. registrar.register(factory, withId: "com.compdfkit.flutter.ui.pdfviewer")
  16. }
  17. public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
  18. switch call.method {
  19. case "sdk_version_code":
  20. result(CPDFKit.sharedInstance().versionNumber)
  21. case "init_sdk":
  22. let initInfo = call.arguments as? [String: Any]
  23. let key = initInfo?["key"] ?? ""
  24. let code = CPDFKit.verify(withKey: key as? String)
  25. print("Code \(code)")
  26. case "init_sdk_keys":
  27. let initInfo = call.arguments as? [String: Any]
  28. let key = initInfo?["iosOnlineLicense"] ?? ""
  29. CPDFKit.verify(withOnlineLicense: key as? String) { code, message in
  30. print("Code: \(code), Message:\(String(describing: message))")
  31. }
  32. case "sdk_build_tag":
  33. result("iOS build tag:\(CPDFKit.sharedInstance().buildNumber)")
  34. case "open_document":
  35. let initInfo = call.arguments as? [String: Any]
  36. let jsonString = initInfo?["configuration"] ?? ""
  37. _ = initInfo?["password"] ?? ""
  38. let path = initInfo?["document"] as? String ?? ""
  39. let document = NSURL(fileURLWithPath: path)
  40. var success = false
  41. var documentPath = path
  42. if startAccessing {
  43. success = document.startAccessingSecurityScopedResource()
  44. startAccessing = false
  45. } else {
  46. let fileManager = FileManager.default
  47. let samplesFilePath = NSHomeDirectory().appending("/Documents/Files")
  48. let fileName = document.lastPathComponent ?? ""
  49. let docsFilePath = samplesFilePath + "/" + fileName
  50. if !fileManager.fileExists(atPath: samplesFilePath) {
  51. try? FileManager.default.createDirectory(atPath: samplesFilePath, withIntermediateDirectories: true, attributes: nil)
  52. }
  53. try? FileManager.default.copyItem(atPath: document.path ?? "", toPath: docsFilePath)
  54. documentPath = docsFilePath
  55. }
  56. let jsonDataParse = CPDFJSONDataParse(String: jsonString as! String)
  57. guard let configuration = jsonDataParse.configuration else { return }
  58. if let rootViewControl = UIApplication.shared.keyWindow?.rootViewController {
  59. var tRootViewControl = rootViewControl
  60. if let presentedViewController = rootViewControl.presentedViewController {
  61. tRootViewControl = presentedViewController
  62. }
  63. let pdfViewController = CPDFViewController(filePath: documentPath, password: nil, configuration: configuration)
  64. let navController = CNavigationController(rootViewController: pdfViewController)
  65. pdfViewController.delegate = self
  66. navController.modalPresentationStyle = .fullScreen
  67. tRootViewControl.present(navController, animated: true)
  68. }
  69. if success {
  70. document.stopAccessingSecurityScopedResource()
  71. }
  72. case "get_temporary_directory":
  73. result(self.getTemporaryDirectory())
  74. case "pick_file":
  75. let documentTypes = ["com.adobe.pdf"]
  76. let documentPickerViewController = UIDocumentPickerViewController(documentTypes: documentTypes, in: .open)
  77. documentPickerViewController.delegate = self
  78. UIApplication.presentedViewController()?.present(documentPickerViewController, animated: true, completion: nil)
  79. _reuslt = result
  80. default:
  81. result(FlutterMethodNotImplemented)
  82. }
  83. }
  84. func getTemporaryDirectory() -> String {
  85. let paths = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true)
  86. return paths.first ?? ""
  87. }
  88. // MARK: - CPDFViewBaseControllerDelete
  89. public func PDFViewBaseControllerDissmiss(_ baseControllerDelete: CPDFViewBaseController) {
  90. baseControllerDelete.dismiss(animated: true)
  91. }
  92. // MARK: - UIDocumentPickerDelegate
  93. public func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
  94. let fileUrlAuthozied = urls.first?.startAccessingSecurityScopedResource() ?? false
  95. if fileUrlAuthozied {
  96. let filePath = urls.first?.path ?? ""
  97. _reuslt?(filePath)
  98. startAccessing = true
  99. }
  100. }
  101. }