123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- import Flutter
- import UIKit
- import ComPDFKit
- import ComPDFKit_Tools
- public class CompdfkitFlutterPlugin: NSObject, FlutterPlugin, CPDFViewBaseControllerDelete, UIDocumentPickerDelegate {
- public var messager : FlutterBinaryMessenger?
-
- private var _reuslt: FlutterResult?
-
- private var pdfViewController: CPDFViewController?
- public static func register(with registrar: FlutterPluginRegistrar) {
- let channel = FlutterMethodChannel.init(name: "com.compdfkit.flutter.plugin", binaryMessenger: registrar.messenger())
- let instance = CompdfkitFlutterPlugin()
- instance.messager = registrar.messenger()
- registrar.addMethodCallDelegate(instance, channel: channel)
- let factory = CPDFViewCtrlFactory(messenger: registrar.messenger())
- registrar.register(factory, withId: "com.compdfkit.flutter.ui.pdfviewer")
- }
-
- public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
- switch call.method {
- case "sdk_version_code":
- result(CPDFKit.sharedInstance().versionNumber)
- case "init_sdk":
- let initInfo = call.arguments as? [String: Any]
- let key = initInfo?["key"] ?? ""
- let code = CPDFKit.verify(withKey: key as? String)
- print("Code \(code)")
- case "init_sdk_keys":
- let initInfo = call.arguments as? [String: Any]
- let key = initInfo?["iosOnlineLicense"] ?? ""
- CPDFKit.verify(withOnlineLicense: key as? String) { code, message in
- print("Code: \(code), Message:\(String(describing: message))")
- }
- case "sdk_build_tag":
- result("iOS build tag:\(CPDFKit.sharedInstance().buildNumber)")
- case "open_document":
- let initInfo = call.arguments as? [String: Any]
- let jsonString = initInfo?["configuration"] ?? ""
- _ = initInfo?["password"] ?? ""
- let path = initInfo?["document"] as? String ?? ""
- let document = NSURL(fileURLWithPath: path)
-
- var success = false
- var documentPath = path
-
- let homeDiectory = NSHomeDirectory()
- let bundlePath = Bundle.main.bundlePath
-
- if (path.hasPrefix(homeDiectory) || path.hasPrefix(bundlePath)) {
- 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)
-
- documentPath = docsFilePath
- } else {
- success = document.startAccessingSecurityScopedResource()
- }
-
-
- let jsonDataParse = CPDFJSONDataParse(String: jsonString as! String)
- guard let configuration = jsonDataParse.configuration else { return }
- if let rootViewControl = UIApplication.shared.keyWindow?.rootViewController {
- var tRootViewControl = rootViewControl
-
- if let presentedViewController = rootViewControl.presentedViewController {
- tRootViewControl = presentedViewController
- }
-
- pdfViewController = CPDFViewController(filePath: documentPath, password: nil, configuration: configuration)
- let navController = CNavigationController(rootViewController: pdfViewController!)
- pdfViewController?.delegate = self
- navController.modalPresentationStyle = .fullScreen
- tRootViewControl.present(navController, animated: true)
- }
-
- if success {
- document.stopAccessingSecurityScopedResource()
- }
- case "get_temporary_directory":
- result(self.getTemporaryDirectory())
-
- case "pick_file":
- let documentTypes = ["com.adobe.pdf"]
- let documentPickerViewController = UIDocumentPickerViewController(documentTypes: documentTypes, in: .open)
- documentPickerViewController.delegate = self
- UIApplication.presentedViewController()?.present(documentPickerViewController, animated: true, completion: nil)
- _reuslt = result
- case "remove_sign_file_list":
- CSignatureManager.sharedManager.removeAllSignatures()
- result(true)
- case "set_import_font_directory":
- let initInfo = call.arguments as? [String: Any]
-
- let dirPath = initInfo?["dir_path"] as? String ?? ""
-
- let addSysFont = initInfo?["add_sys_font"] as? Bool ?? true
-
- let fileManager = FileManager.default
- let documentDirectory = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first!
- let destinationPath = documentDirectory.appendingPathComponent("Font")
- do {
- if fileManager.fileExists(atPath: destinationPath.path) {
- try fileManager.removeItem(at: destinationPath)
- }
- try fileManager.copyItem(atPath: dirPath, toPath: destinationPath.path)
- CPDFFont.setImportDir(destinationPath.path, isContainSysFont: addSysFont)
- } catch {
- print("Error copying Font directory: \(error)")
- }
- result(true)
-
- default:
- result(FlutterMethodNotImplemented)
- }
- }
-
- func getTemporaryDirectory() -> String {
- let paths = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true)
- return paths.first ?? ""
- }
-
- // MARK: - CPDFViewBaseControllerDelete
-
- public func PDFViewBaseControllerDissmiss(_ baseControllerDelete: CPDFViewBaseController) {
- baseControllerDelete.dismiss(animated: true)
- }
-
- // MARK: - UIDocumentPickerDelegate
-
- public func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
- let fileUrlAuthozied = urls.first?.startAccessingSecurityScopedResource() ?? false
- if fileUrlAuthozied {
- let filePath = urls.first?.path ?? ""
- _reuslt?(filePath)
- }
- }
-
- }
|