我正在尝试在线程中打开文件,这是我的代码:
DispatchQueue.main.async(execute: { () -> Void in
var documentsURL = (FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)).appendPathComponent(“File.pdf")
self.docController = UIDocumentInteractionController.init(url: documentsURL as URL)
self.docController?.delegate = self as? UIDocumentInteractionControllerDelegate
self.docController?.presentPreview(animated: true)
self.docController?.presentOpenInMenu(from: CGRect.zero, in: self.view, animated: true)
})
Run Code Online (Sandbox Code Playgroud)
当移动到主屏幕时,会显示此警告并且文件未打开
Warning: Attempt to present <_UIDocumentActivityViewController: 0x...> on <HCM.PrintVacationDecisionVC: 0x...> whose view is not in the window hierarchy!
Run Code Online (Sandbox Code Playgroud)
有什么帮助解决这个问题?
在您的应用程序中添加下面的扩展名,并在任何您想要呈现任何视图控制器的地方使用它,它对我有用,希望它对您有所帮助.
//MARK: - UIApplication Extension
extension UIApplication {
class func topViewController(viewController: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController? {
if let nav = viewController as? UINavigationController {
return topViewController(viewController: nav.visibleViewController)
}
if let tab = viewController as? UITabBarController {
if let selected = tab.selectedViewController {
return topViewController(viewController: selected)
}
}
if let presented = viewController?.presentedViewController {
return topViewController(viewController: presented)
}
return viewController
}
}
Run Code Online (Sandbox Code Playgroud)
并通过以下代码呈现它:
UIApplication.topViewController()?.present(vc, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4720 次 |
| 最近记录: |