|
@@ -16,9 +16,11 @@ import ComPDFKit_Tools
|
|
|
*
|
|
|
*/
|
|
|
@objc(ComPDFKit)
|
|
|
-class ComPDFKit: NSObject, CPDFViewBaseControllerDelete{
|
|
|
+class ComPDFKit: NSObject, CPDFViewBaseControllerDelete, UIDocumentPickerDelegate{
|
|
|
|
|
|
private var pdfViewController: CPDFViewController?
|
|
|
+
|
|
|
+ private var _resolve: RCTPromiseResolveBlock?
|
|
|
|
|
|
/**
|
|
|
* Get the version number of the ComPDFKit SDK.<br/>
|
|
@@ -132,30 +134,66 @@ class ComPDFKit: NSObject, CPDFViewBaseControllerDelete{
|
|
|
@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)
|
|
|
- }
|
|
|
+ var documentPath = document.path
|
|
|
+ var success = false
|
|
|
+
|
|
|
+ let homeDiectory = NSHomeDirectory()
|
|
|
+
|
|
|
+ if (documentPath.hasPrefix(homeDiectory)) {
|
|
|
+ let fileManager = FileManager.default
|
|
|
+ let samplesFilePath = NSHomeDirectory().appending("/Documents/Files")
|
|
|
+ let fileName = document.lastPathComponent
|
|
|
+ let docsFilePath = samplesFilePath + "/" + fileName
|
|
|
|
|
|
- try? FileManager.default.copyItem(atPath: document.path, toPath: docsFilePath)
|
|
|
+ if !fileManager.fileExists(atPath: samplesFilePath) {
|
|
|
+ try? FileManager.default.createDirectory(atPath: samplesFilePath, withIntermediateDirectories: true, attributes: nil)
|
|
|
+ }
|
|
|
|
|
|
+ try? FileManager.default.copyItem(atPath: document.path, toPath: docsFilePath)
|
|
|
+
|
|
|
+ documentPath = docsFilePath
|
|
|
+ } else {
|
|
|
+ success = document.startAccessingSecurityScopedResource()
|
|
|
+ }
|
|
|
+
|
|
|
let rootNav = ComPDFKit.presentedViewController()
|
|
|
|
|
|
let jsonDataParse = CPDFJSONDataParse(String: configurationJson)
|
|
|
guard let configuration = jsonDataParse.configuration else { return }
|
|
|
|
|
|
- self.pdfViewController = CPDFViewController(filePath: docsFilePath, password: password, configuration: configuration)
|
|
|
+ self.pdfViewController = CPDFViewController(filePath: documentPath, password: password, configuration: configuration)
|
|
|
self.pdfViewController?.delegate = self
|
|
|
let nav = CNavigationController(rootViewController: self.pdfViewController!)
|
|
|
nav.modalPresentationStyle = .fullScreen
|
|
|
rootNav?.present(nav, animated: true)
|
|
|
+
|
|
|
+ if success {
|
|
|
+ document.stopAccessingSecurityScopedResource()
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @objc(removeSignFileList:withRejecter:)
|
|
|
+ func removeSignFileList(resolve: @escaping RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) {
|
|
|
+ DispatchQueue.main.async {
|
|
|
+ self.pdfViewController?.removeAllElectronicSignatures()
|
|
|
+ resolve(true)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @objc(pickFile:withRejecter:)
|
|
|
+ func pickFile(resolve: @escaping RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) {
|
|
|
+ DispatchQueue.main.async {
|
|
|
+ let documentTypes = ["com.adobe.pdf"]
|
|
|
+ let documentPickerViewController = UIDocumentPickerViewController(documentTypes: documentTypes, in: .open)
|
|
|
+ documentPickerViewController.delegate = self
|
|
|
+ UIApplication.presentedViewController()?.present(documentPickerViewController, animated: true, completion: nil)
|
|
|
+ self._resolve = resolve
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ //MARK: - ViewController Method
|
|
|
|
|
|
/**
|
|
|
* CPDFViewBaseControllerDelete delegate to dismiss ViewController.<br/>
|
|
@@ -209,11 +247,15 @@ class ComPDFKit: NSObject, CPDFViewBaseControllerDelete{
|
|
|
|
|
|
return currentViewController
|
|
|
}
|
|
|
-
|
|
|
- @objc(removeSignFileList:withRejecter:)
|
|
|
- func removeSignFileList(resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) {
|
|
|
- self.pdfViewController?.removeAllElectronicSignatures()
|
|
|
- resolve(true)
|
|
|
+
|
|
|
+ //MARK: - UIDocumentPickerDelegate
|
|
|
+
|
|
|
+ func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
|
|
|
+ let fileUrlAuthozied = urls.first?.startAccessingSecurityScopedResource() ?? false
|
|
|
+ if fileUrlAuthozied {
|
|
|
+ let filePath = urls.first?.path ?? ""
|
|
|
+ self._resolve?(filePath)
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
}
|