CompdfkitFlutterPlugin.swift 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 _reuslt: FlutterResult?
  8. private var pdfViewController: CPDFViewController?
  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. let homeDiectory = NSHomeDirectory()
  43. let bundlePath = Bundle.main.bundlePath
  44. if (path.hasPrefix(homeDiectory) || path.hasPrefix(bundlePath)) {
  45. let fileManager = FileManager.default
  46. let samplesFilePath = NSHomeDirectory().appending("/Documents/Files")
  47. let fileName = document.lastPathComponent ?? ""
  48. let docsFilePath = samplesFilePath + "/" + fileName
  49. if !fileManager.fileExists(atPath: samplesFilePath) {
  50. try? FileManager.default.createDirectory(atPath: samplesFilePath, withIntermediateDirectories: true, attributes: nil)
  51. }
  52. try? FileManager.default.copyItem(atPath: document.path ?? "", toPath: docsFilePath)
  53. documentPath = docsFilePath
  54. } else {
  55. success = document.startAccessingSecurityScopedResource()
  56. }
  57. let jsonDataParse = CPDFJSONDataParse(String: jsonString as! String)
  58. guard let configuration = jsonDataParse.configuration else { return }
  59. if let rootViewControl = UIApplication.shared.keyWindow?.rootViewController {
  60. var tRootViewControl = rootViewControl
  61. if let presentedViewController = rootViewControl.presentedViewController {
  62. tRootViewControl = presentedViewController
  63. }
  64. pdfViewController = CPDFViewController(filePath: documentPath, password: nil, configuration: configuration)
  65. let navController = CNavigationController(rootViewController: pdfViewController!)
  66. pdfViewController?.delegate = self
  67. navController.modalPresentationStyle = .fullScreen
  68. tRootViewControl.present(navController, animated: true)
  69. }
  70. if success {
  71. document.stopAccessingSecurityScopedResource()
  72. }
  73. case "get_temporary_directory":
  74. result(self.getTemporaryDirectory())
  75. case "pick_file":
  76. let documentTypes = ["com.adobe.pdf"]
  77. let documentPickerViewController = UIDocumentPickerViewController(documentTypes: documentTypes, in: .open)
  78. documentPickerViewController.delegate = self
  79. UIApplication.presentedViewController()?.present(documentPickerViewController, animated: true, completion: nil)
  80. _reuslt = result
  81. case "remove_sign_file_list":
  82. CSignatureManager.sharedManager.removeAllSignatures()
  83. result(true)
  84. case "set_import_font_directory":
  85. let initInfo = call.arguments as? [String: Any]
  86. let dirPath = initInfo?["dir_path"] as? String ?? ""
  87. let addSysFont = initInfo?["add_sys_font"] as? Bool ?? true
  88. let fileManager = FileManager.default
  89. let documentDirectory = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first!
  90. let destinationPath = documentDirectory.appendingPathComponent("Font")
  91. do {
  92. if fileManager.fileExists(atPath: destinationPath.path) {
  93. try fileManager.removeItem(at: destinationPath)
  94. }
  95. try fileManager.copyItem(atPath: dirPath, toPath: destinationPath.path)
  96. CPDFFont.setImportDir(destinationPath.path, isContainSysFont: addSysFont)
  97. } catch {
  98. print("Error copying Font directory: \(error)")
  99. }
  100. result(true)
  101. default:
  102. result(FlutterMethodNotImplemented)
  103. }
  104. }
  105. func getTemporaryDirectory() -> String {
  106. let paths = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true)
  107. return paths.first ?? ""
  108. }
  109. // MARK: - CPDFViewBaseControllerDelete
  110. public func PDFViewBaseControllerDissmiss(_ baseControllerDelete: CPDFViewBaseController) {
  111. baseControllerDelete.dismiss(animated: true)
  112. }
  113. // MARK: - UIDocumentPickerDelegate
  114. public func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
  115. let fileUrlAuthozied = urls.first?.startAccessingSecurityScopedResource() ?? false
  116. if fileUrlAuthozied {
  117. let filePath = urls.first?.path ?? ""
  118. _reuslt?(filePath)
  119. }
  120. }
  121. }