CompdfkitFlutterPlugin.swift 4.0 KB

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