ComPDFKitRN.swift 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 fileManager = FileManager.default
  123. let samplesFilePath = NSHomeDirectory().appending("/Documents/BasicViewer")
  124. let fileName = document.lastPathComponent
  125. let docsFilePath = samplesFilePath + "/" + fileName
  126. if !fileManager.fileExists(atPath: samplesFilePath) {
  127. try? FileManager.default.createDirectory(atPath: samplesFilePath, withIntermediateDirectories: true, attributes: nil)
  128. }
  129. try? FileManager.default.copyItem(atPath: document.path, toPath: docsFilePath)
  130. let rootNav = ComPDFKit.presentedViewController()
  131. let jsonDataParse = CPDFJSONDataParse(String: configurationJson)
  132. guard let configuration = jsonDataParse.configuration else { return }
  133. let pdfViewController = CPDFViewController(filePath: docsFilePath, password: password, configuration: configuration)
  134. pdfViewController.delegate = self
  135. let nav = CNavigationController(rootViewController: pdfViewController)
  136. nav.modalPresentationStyle = .fullScreen
  137. rootNav?.present(nav, animated: true)
  138. }
  139. }
  140. /**
  141. * CPDFViewBaseControllerDelete delegate to dismiss ViewController.<br/>
  142. */
  143. func PDFViewBaseControllerDissmiss(_ baseControllerDelete: CPDFViewBaseController) {
  144. baseControllerDelete.dismiss(animated: true)
  145. }
  146. /**
  147. * Cet a root ViewController.<br/>
  148. */
  149. class func presentedViewController() -> UIViewController? {
  150. var rootViewController: UIViewController? = nil
  151. if let appDelegate = UIApplication.shared.delegate as? NSObject {
  152. if appDelegate.responds(to: Selector(("viewController"))) {
  153. rootViewController = appDelegate.value(forKey: "viewController") as? UIViewController
  154. }
  155. }
  156. if rootViewController == nil, let appDelegate = UIApplication.shared.delegate as? NSObject, appDelegate.responds(to: #selector(getter: UIApplicationDelegate.window)) {
  157. if let window = appDelegate.value(forKey: "window") as? UIWindow {
  158. rootViewController = window.rootViewController
  159. }
  160. }
  161. if rootViewController == nil {
  162. if let window = UIApplication.shared.keyWindow {
  163. rootViewController = window.rootViewController
  164. }
  165. }
  166. guard let finalRootViewController = rootViewController else {
  167. return nil
  168. }
  169. var currentViewController = finalRootViewController
  170. while let presentedViewController = currentViewController.presentedViewController {
  171. if !(presentedViewController is UIAlertController) && currentViewController.modalPresentationStyle != .popover {
  172. currentViewController = presentedViewController
  173. } else {
  174. return currentViewController
  175. }
  176. }
  177. return currentViewController
  178. }
  179. }