Commit 15cd397c authored by Alex朱枝文's avatar Alex朱枝文

解决文件查看工具可能出现崩溃的问题

https://bugly.qq.com/v2/crash-reporting/crashes/5dc2acc53e?pid=2
parent adcd4459
......@@ -10,9 +10,10 @@ import UIKit
class YHFilePreviewTool: NSObject {
weak var targetVC: UIViewController!
weak var targetVC: UIViewController?
private var documentController: UIDocumentInteractionController?
init(targetVC: UIViewController!) {
init(targetVC: UIViewController?) {
self.targetVC = targetVC
}
......@@ -50,13 +51,22 @@ class YHFilePreviewTool: NSObject {
}
func openXLSXFile(at fileURL: URL, fileName: String?) {
let controller = UIDocumentInteractionController(url: fileURL)
documentController = UIDocumentInteractionController(url: fileURL)
// 设置标题显示
if let fileName = fileName, !fileName.isEmpty {
controller.name = fileName
documentController?.name = fileName
}
controller.delegate = self
DispatchQueue.main.async {
documentController?.delegate = self
guard let controller = documentController else {
printLog("Failed to create UIDocumentInteractionController")
return
}
DispatchQueue.main.async { [weak self] in
guard let self = self, let presentingVC = self.targetVC else {
printLog("Target view controller is not available")
return
}
controller.presentPreview(animated: true)
}
}
......@@ -64,6 +74,6 @@ class YHFilePreviewTool: NSObject {
extension YHFilePreviewTool: UIDocumentInteractionControllerDelegate {
func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController {
return targetVC
return targetVC ?? UIViewController.current ?? UIViewController()
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment