CPDFViewCtrlFactory.swift 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 plugin : CPDFViewCtrlPlugin
  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 document = NSURL(fileURLWithPath: path)
  51. var success = false
  52. var documentPath = path
  53. let homeDiectory = NSHomeDirectory()
  54. let bundlePath = Bundle.main.bundlePath
  55. if (path.hasPrefix(homeDiectory) || path.hasPrefix(bundlePath)) {
  56. let fileManager = FileManager.default
  57. let samplesFilePath = NSHomeDirectory().appending("/Documents/Files")
  58. let fileName = document.lastPathComponent ?? ""
  59. let docsFilePath = samplesFilePath + "/" + fileName
  60. if !fileManager.fileExists(atPath: samplesFilePath) {
  61. try? FileManager.default.createDirectory(atPath: samplesFilePath, withIntermediateDirectories: true, attributes: nil)
  62. }
  63. try? FileManager.default.copyItem(atPath: document.path ?? "", toPath: docsFilePath)
  64. documentPath = docsFilePath
  65. } else {
  66. success = document.startAccessingSecurityScopedResource()
  67. }
  68. let jsonDataParse = CPDFJSONDataParse(String: jsonString as! String)
  69. let configuration = jsonDataParse.configuration
  70. // Create the pdfview controller view
  71. pdfViewController = CPDFViewController(filePath: documentPath, password: password as? String, configuration: configuration!)
  72. navigationController = CNavigationController(rootViewController: pdfViewController)
  73. navigationController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  74. navigationController.view.frame = frame
  75. plugin = CPDFViewCtrlPlugin(viewId: viewId, binaryMessenger: messenger!, controller: pdfViewController)
  76. super.init()
  77. // Proxy set, but not used
  78. pdfViewController.delegate = self
  79. navigationController.setViewControllers([pdfViewController], animated: true)
  80. if success {
  81. document.stopAccessingSecurityScopedResource()
  82. }
  83. }
  84. func view() -> UIView {
  85. return navigationController.view
  86. }
  87. // MARK: - CPDFViewBaseControllerDelete
  88. public func PDFViewBaseControllerDissmiss(_ baseControllerDelete: CPDFViewBaseController) {
  89. }
  90. func PDFViewBaseController(_ baseController: CPDFViewBaseController, SaveState success: Bool) {
  91. if success {
  92. plugin._methodChannel.invokeMethod("saveDocument", arguments: nil)
  93. }
  94. }
  95. func PDFViewBaseController(_ baseController: CPDFViewBaseController, currentPageIndex index: Int) {
  96. plugin._methodChannel.invokeMethod("onPageChanged", arguments: ["pageIndex": index])
  97. }
  98. }