OpenPDFModule.swift 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. //
  2. // OpenPDFModule.swift
  3. // ComPDFKit_RN
  4. //
  5. // Copyright © 2014-2023 PDF Technologies, Inc. All Rights Reserved.
  6. //
  7. // THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
  8. // AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
  9. // UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
  10. // This notice may not be removed from this file.
  11. //
  12. import UIKit
  13. import Foundation
  14. import ComPDFKit
  15. import ComPDFKit_Tools
  16. @objc(OpenPDFModule)
  17. class OpenPDFModule: NSObject, CPDFViewBaseControllerDelete {
  18. @objc(openPDF:)
  19. func openPDF(_ configurationJson: String) -> Void {
  20. DispatchQueue.main.async {
  21. let documentPath = Bundle.main.path(forResource: "developer_guide_ios", ofType: "pdf") ?? ""
  22. let rootNav = OpenPDFModule.presentedViewController()
  23. let jsonDataParse = CPDFJSONDataParse(String: configurationJson)
  24. guard let configuration = jsonDataParse.configuration else { return }
  25. let pdfViewController = CPDFViewController(filePath: documentPath, password: "", configuration: configuration)
  26. pdfViewController.delegate = self
  27. let nav = CNavigationController(rootViewController: pdfViewController)
  28. nav.modalPresentationStyle = .fullScreen
  29. rootNav?.present(nav, animated: true)
  30. }
  31. }
  32. @objc(openPDFByConfiguration: password: configurationJson:)
  33. func configurationJson(filePath: String, password: String, configurationJson: String) {
  34. let rootNav = OpenPDFModule.presentedViewController()
  35. let jsonDataParse = CPDFJSONDataParse(String: configurationJson)
  36. guard let configuration = jsonDataParse.configuration else { return }
  37. let pdfViewController = CPDFViewController(filePath: filePath, password: password, configuration: configuration)
  38. pdfViewController.delegate = self
  39. let nav = CNavigationController(rootViewController: pdfViewController)
  40. nav.modalPresentationStyle = .fullScreen
  41. rootNav?.present(nav, animated: true)
  42. }
  43. @objc static func requiresMainQueueSetup() -> Bool {
  44. return true
  45. }
  46. func PDFViewBaseControllerDissmiss(_ baseControllerDelete: CPDFViewBaseController) {
  47. baseControllerDelete.dismiss(animated: true)
  48. }
  49. class func presentedViewController() -> UIViewController? {
  50. var rootViewController: UIViewController? = nil
  51. if let appDelegate = UIApplication.shared.delegate as? NSObject {
  52. if appDelegate.responds(to: Selector(("viewController"))) {
  53. rootViewController = appDelegate.value(forKey: "viewController") as? UIViewController
  54. }
  55. }
  56. if rootViewController == nil, let appDelegate = UIApplication.shared.delegate as? NSObject, appDelegate.responds(to: #selector(getter: UIApplicationDelegate.window)) {
  57. if let window = appDelegate.value(forKey: "window") as? UIWindow {
  58. rootViewController = window.rootViewController
  59. }
  60. }
  61. if rootViewController == nil {
  62. if let window = UIApplication.shared.keyWindow {
  63. rootViewController = window.rootViewController
  64. }
  65. }
  66. guard let finalRootViewController = rootViewController else {
  67. return nil
  68. }
  69. var currentViewController = finalRootViewController
  70. while let presentedViewController = currentViewController.presentedViewController {
  71. if !(presentedViewController is UIAlertController) && currentViewController.modalPresentationStyle != .popover {
  72. currentViewController = presentedViewController
  73. } else {
  74. return currentViewController
  75. }
  76. }
  77. return currentViewController
  78. }
  79. }