CPDFViewCtrlFactory.swift 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. //
  2. // CPDFViewCtrlFactory.swift
  3. //
  4. // Copyright © 2014-2023 PDF Technologies, Inc. All Rights Reserved.
  5. //
  6. // THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
  7. // AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
  8. // UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
  9. // This notice may not be removed from this file.
  10. import Flutter
  11. import UIKit
  12. import ComPDFKit
  13. import ComPDFKit_Tools
  14. class CPDFViewCtrlFactory: NSObject, FlutterPlatformViewFactory {
  15. private let messenger: FlutterBinaryMessenger
  16. init(messenger: FlutterBinaryMessenger) {
  17. self.messenger = messenger
  18. super.init()
  19. }
  20. func create(
  21. withFrame frame: CGRect,
  22. viewIdentifier viewId: Int64,
  23. arguments args: Any?
  24. ) -> FlutterPlatformView {
  25. return CPDFViewCtrlFlutter(
  26. frame: frame,
  27. viewIdentifier: viewId,
  28. arguments: args,
  29. binaryMessenger: messenger)
  30. }
  31. func createArgsCodec() -> FlutterMessageCodec & NSObjectProtocol {
  32. return FlutterStandardMessageCodec.sharedInstance()
  33. }
  34. }
  35. class CPDFViewCtrlFlutter: NSObject, FlutterPlatformView, CPDFViewBaseControllerDelete {
  36. private var pdfViewController : CPDFViewController
  37. private var navigationController : CNavigationController
  38. private var _methodChannel : FlutterMethodChannel
  39. init(
  40. frame: CGRect,
  41. viewIdentifier viewId: Int64,
  42. arguments args: Any?,
  43. binaryMessenger messenger: FlutterBinaryMessenger?
  44. ) {
  45. // Parses the document path, password, and configuration information
  46. let initInfo = args as? [String: Any]
  47. let jsonString = initInfo?["configuration"] ?? ""
  48. let password = initInfo?["password"] ?? ""
  49. let path = initInfo?["document"] as? String ?? ""
  50. let jsonDataParse = CPDFJSONDataParse(String: jsonString as! String)
  51. let configuration = jsonDataParse.configuration
  52. // Create the pdfview controller view
  53. pdfViewController = CPDFViewController(filePath: path, password: password as? String, configuration: configuration!)
  54. navigationController = CNavigationController(rootViewController: pdfViewController)
  55. navigationController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  56. navigationController.view.frame = frame
  57. _methodChannel = FlutterMethodChannel.init(name: "com.compdfkit.flutter.ui.pdfviewer.\(viewId)", binaryMessenger: messenger!)
  58. super.init()
  59. // Proxy set, but not used
  60. pdfViewController.delegate = self
  61. navigationController.setViewControllers([pdfViewController], animated: false)
  62. registeryMethodChannel(viewId: viewId, binaryMessenger: messenger!)
  63. }
  64. func view() -> UIView {
  65. return navigationController.view
  66. }
  67. public func PDFViewBaseControllerDissmiss(_ baseControllerDelete: CPDFViewBaseController) {
  68. baseControllerDelete.dismiss(animated: true)
  69. }
  70. private func registeryMethodChannel(viewId: Int64, binaryMessenger messenger: FlutterBinaryMessenger){
  71. _methodChannel.setMethodCallHandler({
  72. (call: FlutterMethodCall, result: FlutterResult) -> Void in
  73. print("ComPDFKit-Flutter: iOS-MethodChannel: [method:\(call.method)]")
  74. // Handle battery messages.
  75. switch call.method {
  76. case "save":
  77. // save pdf
  78. guard let pdfListView = self.pdfViewController.pdfListView else {
  79. result(true)
  80. return
  81. }
  82. var isSuccess = false
  83. if (pdfListView.isEditing() == true && pdfListView.isEdited() == true) {
  84. pdfListView.commitEditing()
  85. if pdfListView.document.isModified() == true {
  86. isSuccess = pdfListView.document.write(to: pdfListView.document.documentURL)
  87. }
  88. } else {
  89. if(pdfListView.document != nil) {
  90. if pdfListView.document.isModified() == true {
  91. isSuccess = pdfListView.document.write(to: pdfListView.document.documentURL)
  92. }
  93. }
  94. }
  95. result(isSuccess) // or return false
  96. default:
  97. result(FlutterMethodNotImplemented)
  98. }
  99. });
  100. }
  101. }