CompdfkitFlutterPlugin.swift 3.9 KB

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