OpenPDFModule.swift 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. //
  2. // OpenPDFModule.swift
  3. // ComPDFKit_RN
  4. //
  5. // Copyright © 2014-2024 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 openPDFByConfiguration(filePath: URL, password: String, configurationJson: String) {
  34. DispatchQueue.main.async {
  35. let rootNav = OpenPDFModule.presentedViewController()
  36. let jsonDataParse = CPDFJSONDataParse(String: configurationJson)
  37. guard let configuration = jsonDataParse.configuration else { return }
  38. let pdfViewController = CPDFViewController(filePath: filePath.path, password: password, configuration: configuration)
  39. pdfViewController.delegate = self
  40. let nav = CNavigationController(rootViewController: pdfViewController)
  41. nav.modalPresentationStyle = .fullScreen
  42. rootNav?.present(nav, animated: true)
  43. }
  44. }
  45. @objc(init_:)
  46. func init_(_ license: String) {
  47. DispatchQueue.main.async {
  48. CPDFKit.verify(withKey: license)
  49. }
  50. }
  51. @objc(initialize: iosOnlineLicense:)
  52. func initialize(_ androidOnlineLicense: String, iosOnlineLicense: String) {
  53. DispatchQueue.main.async {
  54. CPDFKit.verify(withOnlineLicense: iosOnlineLicense) { code, message in
  55. print("Code: \(code), Message:\(String(describing: message))")
  56. }
  57. }
  58. }
  59. @objc static func requiresMainQueueSetup() -> Bool {
  60. return true
  61. }
  62. func PDFViewBaseControllerDissmiss(_ baseControllerDelete: CPDFViewBaseController) {
  63. baseControllerDelete.dismiss(animated: true)
  64. }
  65. class func presentedViewController() -> UIViewController? {
  66. var rootViewController: UIViewController? = nil
  67. if let appDelegate = UIApplication.shared.delegate as? NSObject {
  68. if appDelegate.responds(to: Selector(("viewController"))) {
  69. rootViewController = appDelegate.value(forKey: "viewController") as? UIViewController
  70. }
  71. }
  72. if rootViewController == nil, let appDelegate = UIApplication.shared.delegate as? NSObject, appDelegate.responds(to: #selector(getter: UIApplicationDelegate.window)) {
  73. if let window = appDelegate.value(forKey: "window") as? UIWindow {
  74. rootViewController = window.rootViewController
  75. }
  76. }
  77. if rootViewController == nil {
  78. if let window = UIApplication.shared.keyWindow {
  79. rootViewController = window.rootViewController
  80. }
  81. }
  82. guard let finalRootViewController = rootViewController else {
  83. return nil
  84. }
  85. var currentViewController = finalRootViewController
  86. while let presentedViewController = currentViewController.presentedViewController {
  87. if !(presentedViewController is UIAlertController) && currentViewController.modalPresentationStyle != .popover {
  88. currentViewController = presentedViewController
  89. } else {
  90. return currentViewController
  91. }
  92. }
  93. return currentViewController
  94. }
  95. }