// Copyright © 2014-2024 PDF Technologies, Inc. All Rights Reserved. // // THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW // AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT. // UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES. // This notice may not be removed from this file. // import UIKit import Foundation import ComPDFKit import ComPDFKit_Tools /** * RN and iOS native ComPDFKit SDK interaction class * */ @objc(ComPDFKit) class ComPDFKit: NSObject, CPDFViewBaseControllerDelete{ /** * Get the version number of the ComPDFKit SDK.
* For example: "2.0.0".
*

* Usage example:

*
      * ComPDFKit.getVersionCode().then((versionCode : string) => {
      *   console.log('ComPDFKit SDK Version:', versionCode)
      * })
      * 
* */ @objc(getVersionCode:withRejecter:) func getVersionCode(resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) -> Void { resolve(String(CPDFKit.sharedInstance().versionNumber)) } /** * Get the build tag of the ComPDFKit PDF SDK.
* For example: "build_beta_2.0.0_42db96987_202404081007"
*

* * Usage example:
*
       * ComPDFKit.getSDKBuildTag().then((buildTag : string) => {
       *   console.log('ComPDFKit Build Tag:', buildTag)
       * })
       * 
* */ @objc(getSDKBuildTag:withRejecter:) func getSDKBuildTag(resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) -> Void { let sdkBuildTag = CPDFKit.sharedInstance().versionString resolve(sdkBuildTag) } /** * Initialize the ComPDFKit PDF SDK using offline authentication.
*

* Usage example:
*
     * ComPDFKit.init_('license')
     * 
* * @param license The offline license. */ @objc(init_: withResolver: withRejecter:) func init_(license : String,resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock){ DispatchQueue.main.async { let code = CPDFKit.verify(withKey: license) print("ComPDFKitRN-iOS init_:\(code)") resolve(code == CPDFKitLicenseCode.success) } } /** * Initialize the ComPDFKit PDF SDK using online authentication.
* Requires internet connection. Please ensure that the network permission has been added in [AndroidManifest.xml] file.
* {@link android.Manifest.permission#INTERNET}
*

* Usage example: *
     *   ComPDFKit.initialize(androidLicense, iosLicense)
     * 
* * @param androidOnlineLicense The online license for the ComPDFKit SDK on Android platform. * @param iosOnlineLicense The online license for the ComPDFKit SDK on iOS platform. */ @objc(initialize: iosOnlineLicense: withResolver: withRejecter:) func initialize(_ androidOnlineLicense: String, iosOnlineLicense: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) { DispatchQueue.main.async { CPDFKit.verify(withOnlineLicense: iosOnlineLicense) { code, message in print("ComPDFKitRN-iOS initialize: \(code), Message:\(String(describing: message))") resolve(code == CPDFKitOnlineLicenseCode.success) } } } /** * Display a PDF.
* * Usage example:
*
       *   ComPDFKit.openDocument(document, password, configurationJson)
       * 
* * (Android) For local storage file path:
*
       *   document = "file:///storage/emulated/0/Download/sample.pdf";
*
* * (Android) For content Uri:
*
       *   document = "content://...";
       * 
* * (Android) For assets path:
*
       *   document = "file:///android_asset/..."
       * 
* * @param document The document URI or file path. * @param password The document password. * @param configurationJson Configuration data in JSON format. */ @objc(openDocument: password: configurationJson:) func openDocument(document : URL, password: String, configurationJson : String) -> Void { DispatchQueue.main.async { let fileManager = FileManager.default let samplesFilePath = NSHomeDirectory().appending("/Documents/Files") let fileName = document.lastPathComponent let docsFilePath = samplesFilePath + "/" + fileName if !fileManager.fileExists(atPath: samplesFilePath) { try? FileManager.default.createDirectory(atPath: samplesFilePath, withIntermediateDirectories: true, attributes: nil) } try? FileManager.default.copyItem(atPath: document.path, toPath: docsFilePath) let rootNav = ComPDFKit.presentedViewController() let jsonDataParse = CPDFJSONDataParse(String: configurationJson) guard let configuration = jsonDataParse.configuration else { return } let pdfViewController = CPDFViewController(filePath: docsFilePath, password: password, configuration: configuration) pdfViewController.delegate = self let nav = CNavigationController(rootViewController: pdfViewController) nav.modalPresentationStyle = .fullScreen rootNav?.present(nav, animated: true) } } /** * CPDFViewBaseControllerDelete delegate to dismiss ViewController.
*/ func PDFViewBaseControllerDissmiss(_ baseControllerDelete: CPDFViewBaseController) { baseControllerDelete.dismiss(animated: true) } /** * Cet a root ViewController.
*/ class func presentedViewController() -> UIViewController? { var rootViewController: UIViewController? = nil if let appDelegate = UIApplication.shared.delegate as? NSObject { if appDelegate.responds(to: Selector(("viewController"))) { rootViewController = appDelegate.value(forKey: "viewController") as? UIViewController } } if rootViewController == nil, let appDelegate = UIApplication.shared.delegate as? NSObject, appDelegate.responds(to: #selector(getter: UIApplicationDelegate.window)) { if let window = appDelegate.value(forKey: "window") as? UIWindow { rootViewController = window.rootViewController } } if rootViewController == nil { if let window = UIApplication.shared.keyWindow { rootViewController = window.rootViewController } } guard let finalRootViewController = rootViewController else { return nil } var currentViewController = finalRootViewController while let presentedViewController = currentViewController.presentedViewController { if !(presentedViewController is UIAlertController) && currentViewController.modalPresentationStyle != .popover { currentViewController = presentedViewController } else { return currentViewController } } return currentViewController } }