ComPDFKitRN.swift 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. // Copyright © 2014-2024 PDF Technologies, Inc. All Rights Reserved.
  2. //
  3. // THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
  4. // AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
  5. // UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
  6. // This notice may not be removed from this file.
  7. //
  8. import UIKit
  9. import Foundation
  10. import ComPDFKit
  11. import ComPDFKit_Tools
  12. /**
  13. * RN and iOS native ComPDFKit SDK interaction class
  14. *
  15. */
  16. @objc(ComPDFKit)
  17. class ComPDFKit: NSObject, CPDFViewBaseControllerDelete{
  18. /**
  19. * Get the version number of the ComPDFKit SDK.<br/>
  20. * For example: "2.0.0".<br/>
  21. * <p></p>
  22. * Usage example:<br/><br/>
  23. * <pre>
  24. * ComPDFKit.getVersionCode().then((versionCode : string) => {
  25. * console.log('ComPDFKit SDK Version:', versionCode)
  26. * })
  27. * </pre>
  28. *
  29. */
  30. @objc(getVersionCode:withRejecter:)
  31. func getVersionCode(resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) -> Void {
  32. resolve(String(CPDFKit.sharedInstance().versionNumber))
  33. }
  34. /**
  35. * Get the build tag of the ComPDFKit PDF SDK.<br/>
  36. * For example: "build_beta_2.0.0_42db96987_202404081007"<br/>
  37. * <p></p>
  38. *
  39. * Usage example:<br/>
  40. * <pre>
  41. * ComPDFKit.getSDKBuildTag().then((buildTag : string) => {
  42. * console.log('ComPDFKit Build Tag:', buildTag)
  43. * })
  44. * </pre>
  45. *
  46. */
  47. @objc(getSDKBuildTag:withRejecter:)
  48. func getSDKBuildTag(resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) -> Void {
  49. let sdkBuildTag = CPDFKit.sharedInstance().versionString
  50. resolve(sdkBuildTag)
  51. }
  52. /**
  53. * Initialize the ComPDFKit PDF SDK using offline authentication.<br/>
  54. * <p></p>
  55. * Usage example:<br/>
  56. * <pre>
  57. * ComPDFKit.init_('license')
  58. * </pre>
  59. *
  60. * @param license The offline license.
  61. */
  62. @objc(init_: withResolver: withRejecter:)
  63. func init_(license : String,resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock){
  64. DispatchQueue.main.async {
  65. let code = CPDFKit.verify(withKey: license)
  66. print("ComPDFKitRN-iOS init_:\(code)")
  67. resolve(code == CPDFKitLicenseCode.success)
  68. }
  69. }
  70. /**
  71. * Initialize the ComPDFKit PDF SDK using online authentication. <br/>
  72. * Requires internet connection. Please ensure that the network permission has been added in [AndroidManifest.xml] file. <br/>
  73. * {@link android.Manifest.permission#INTERNET} <br/>
  74. * <p></p>
  75. * Usage example:
  76. * <pre>
  77. * ComPDFKit.initialize(androidLicense, iosLicense)
  78. * </pre>
  79. *
  80. * @param androidOnlineLicense The online license for the ComPDFKit SDK on Android platform.
  81. * @param iosOnlineLicense The online license for the ComPDFKit SDK on iOS platform.
  82. */
  83. @objc(initialize: iosOnlineLicense: withResolver: withRejecter:)
  84. func initialize(_ androidOnlineLicense: String, iosOnlineLicense: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
  85. DispatchQueue.main.async {
  86. CPDFKit.verify(withOnlineLicense: iosOnlineLicense) { code, message in
  87. print("ComPDFKitRN-iOS initialize: \(code), Message:\(String(describing: message))")
  88. resolve(code == CPDFKitOnlineLicenseCode.success)
  89. }
  90. }
  91. }
  92. /**
  93. * Display a PDF.<br/>
  94. *
  95. * Usage example:<br/>
  96. * <pre>
  97. * ComPDFKit.openDocument(document, password, configurationJson)
  98. * </pre>
  99. *
  100. * (Android) For local storage file path: <br/>
  101. * <pre>
  102. * document = "file:///storage/emulated/0/Download/sample.pdf";<br/>
  103. * </pre>
  104. *
  105. * (Android) For content Uri: <br/>
  106. * <pre>
  107. * document = "content://...";
  108. * </pre>
  109. *
  110. * (Android) For assets path: <br/>
  111. * <pre>
  112. * document = "file:///android_asset/..."
  113. * </pre>
  114. *
  115. * @param document The document URI or file path.
  116. * @param password The document password.
  117. * @param configurationJson Configuration data in JSON format.
  118. */
  119. @objc(openDocument: password: configurationJson:)
  120. func openDocument(document : URL, password: String, configurationJson : String) -> Void {
  121. DispatchQueue.main.async {
  122. let rootNav = ComPDFKit.presentedViewController()
  123. let jsonDataParse = CPDFJSONDataParse(String: configurationJson)
  124. guard let configuration = jsonDataParse.configuration else { return }
  125. let pdfViewController = CPDFViewController(filePath: document.path, password: password, configuration: configuration)
  126. pdfViewController.delegate = self
  127. let nav = CNavigationController(rootViewController: pdfViewController)
  128. nav.modalPresentationStyle = .fullScreen
  129. rootNav?.present(nav, animated: true)
  130. }
  131. }
  132. /**
  133. * CPDFViewBaseControllerDelete delegate to dismiss ViewController.<br/>
  134. */
  135. func PDFViewBaseControllerDissmiss(_ baseControllerDelete: CPDFViewBaseController) {
  136. baseControllerDelete.dismiss(animated: true)
  137. }
  138. /**
  139. * Cet a root ViewController.<br/>
  140. */
  141. class func presentedViewController() -> UIViewController? {
  142. var rootViewController: UIViewController? = nil
  143. if let appDelegate = UIApplication.shared.delegate as? NSObject {
  144. if appDelegate.responds(to: Selector(("viewController"))) {
  145. rootViewController = appDelegate.value(forKey: "viewController") as? UIViewController
  146. }
  147. }
  148. if rootViewController == nil, let appDelegate = UIApplication.shared.delegate as? NSObject, appDelegate.responds(to: #selector(getter: UIApplicationDelegate.window)) {
  149. if let window = appDelegate.value(forKey: "window") as? UIWindow {
  150. rootViewController = window.rootViewController
  151. }
  152. }
  153. if rootViewController == nil {
  154. if let window = UIApplication.shared.keyWindow {
  155. rootViewController = window.rootViewController
  156. }
  157. }
  158. guard let finalRootViewController = rootViewController else {
  159. return nil
  160. }
  161. var currentViewController = finalRootViewController
  162. while let presentedViewController = currentViewController.presentedViewController {
  163. if !(presentedViewController is UIAlertController) && currentViewController.modalPresentationStyle != .popover {
  164. currentViewController = presentedViewController
  165. } else {
  166. return currentViewController
  167. }
  168. }
  169. return currentViewController
  170. }
  171. }