CompdfkitFlutterPlugin.swift 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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().versionString)
  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 "openDocument":
  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 jsonDataParse = CPDFJSONDataParse(String: jsonString as! String)
  36. guard let configuration = jsonDataParse.configuration else { return }
  37. if let rootViewControl = UIApplication.shared.keyWindow?.rootViewController {
  38. var tRootViewControl = rootViewControl
  39. if let presentedViewController = rootViewControl.presentedViewController {
  40. tRootViewControl = presentedViewController
  41. }
  42. let pdfViewController = CPDFViewController(filePath: path, password: nil, configuration: configuration)
  43. let navController = CNavigationController(rootViewController: pdfViewController)
  44. pdfViewController.delegate = self
  45. navController.modalPresentationStyle = .fullScreen
  46. tRootViewControl.present(navController, animated: true)
  47. }
  48. case "getTemporaryDirectory":
  49. result(self.getTemporaryDirectory())
  50. default:
  51. result(FlutterMethodNotImplemented)
  52. }
  53. }
  54. func getTemporaryDirectory() -> String {
  55. let paths = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true)
  56. return paths.first ?? ""
  57. }
  58. // MARK: - CPDFViewBaseControllerDelete
  59. public func PDFViewBaseControllerDissmiss(_ baseControllerDelete: CPDFViewBaseController) {
  60. baseControllerDelete.dismiss(animated: true)
  61. }
  62. }