OpenFilePlugin.swift 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. //
  2. // OpenFilePlugin.swift
  3. // Runner
  4. //
  5. // Created by FancyLou on 2023/5/23.
  6. //
  7. import UIKit
  8. import Flutter
  9. import QuickLook
  10. class OpenFilePlugin {
  11. //预览文件
  12. private lazy var previewVC: FilePreviewController = {
  13. return FilePreviewController()
  14. }()
  15. //
  16. func handleMethodCallOpenFile(filePath: String, result: @escaping FlutterResult, shareBtnShow: Bool = true, doneTitle: String = "关闭") {
  17. guard let root = (UIApplication.shared.delegate as? AppDelegate)?.window.rootViewController else {
  18. result("fail")
  19. return
  20. }
  21. let currentURL = NSURL(fileURLWithPath: filePath)
  22. if QLPreviewController.canPreview(currentURL) {
  23. self.previewVC.currentFileURLS.removeAll()
  24. self.previewVC.currentFileURLS.append(currentURL)
  25. self.previewVC.reloadData()
  26. if !shareBtnShow { // 为了隐藏分享按钮,需要新创建一个 UIViewController
  27. let previewNoActionVC = FilePreviewNoActionbarViewController()
  28. previewNoActionVC.addChild(self.previewVC)
  29. self.previewVC.view.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
  30. previewNoActionVC.view.addSubview(self.previewVC.view)
  31. previewNoActionVC.doneTitle = doneTitle
  32. previewNoActionVC.modalPresentationStyle = .fullScreen
  33. root.show(previewNoActionVC , sender: nil)
  34. } else {
  35. root.show(self.previewVC , sender: nil)
  36. }
  37. result("success")
  38. } else {
  39. //self.showError(title: "当前文件类型不支持预览!")
  40. result("fail")
  41. }
  42. }
  43. }