代码之家  ›  专栏  ›  技术社区  ›  Mansi

从UIDocumentInteractionController保存到iCloud Drive后切换回应用程序会删除导航层次结构

  •  1
  • Mansi  · 技术社区  · 7 年前

    我正在使用UIDocumentInteractionController将指定URL处的文档保存到iCloud驱动器,但问题是,当我从iCloud保存/取消后切换回应用程序时,我原来的视图控制器不再存在,整个导航层次结构将被删除,根视图控制器将显示。

    我从视图控制器显示的视图控制器中显示菜单中的选项。

    extension ADDTextChatViewController: AddReceiverFileDelegate {
        func downloadTapped(url: String?, cell: AddReceiverTableViewCell) {
            guard let urlString = url else {return}
            shareAction(withURLString: urlString, cell: cell)
        }
    }
    
    extension ADDTextChatViewController {
        func share(url: URL, cell: AddReceiverTableViewCell) {
            documentInteractionController.url = url
            documentInteractionController.uti = url.typeIdentifier ?? "public.data, public.content"
            documentInteractionController.name = url.localizedName ?? url.lastPathComponent
            documentInteractionController.presentOptionsMenu(from: cell.btnDownload.frame, in: view, animated: true)
        }
        func shareAction(withURLString: String, cell: AddReceiverTableViewCell) {
            guard let url = URL(string: withURLString) else { return }
            URLSession.shared.dataTask(with: url) { data, response, error in
                guard let data = data, error == nil else { return }
                let tmpURL = FileManager.default.temporaryDirectory
                    .appendingPathComponent(response?.suggestedFilename ?? "fileName.png")
                do {
                    try data.write(to: tmpURL)
                } catch { print(error) }
                DispatchQueue.main.async {
                    self.share(url: tmpURL, cell: cell)
                }
                }.resume()
        }
    }
    
    extension URL {
        var typeIdentifier: String? {
            return (try? resourceValues(forKeys: [.typeIdentifierKey]))?.typeIdentifier
        }
        var localizedName: String? {
            return (try? resourceValues(forKeys: [.localizedNameKey]))?.localizedName
        }
    }
    

    在从iCloud切换回来后,我应该如何保持在调用此方法的同一视图控制器上?

    2 回复  |  直到 7 年前
        1
  •  4
  •   Mansi    7 年前

    因此,为了维护导航堆栈,需要将导航控制器传递到 UIDocumentInteractionControllerDelegate 方法 documentInteractionControllerViewControllerForPreview

    该委托方法的文件中规定 如果在导航堆栈顶部显示,请提供导航控制器,以便以与平台其余部分一致的方式设置动画

    下面是我的代码在实现这个委托方法之后的样子。

    extension ADDTextChatViewController: UIDocumentInteractionControllerDelegate {
        func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController {
            guard let navVC = self.navigationController else {
                return self
            }
            return navVC
        }
    }
    

    我补充道

    documentInteractionController.presentPreview(animated: true)
    

    而不是

    documentInteractionController.presentOptionsMenu(from: view.frame, in: view, animated: true)
    

    它将首先显示如下文档的预览(请参见,此预览将从 ADDTextChatViewController )

    enter image description here

    如左下角所示,箭头将根据文档类型提供如下选项

    enter image description here

    因此,现在当我在转换到iCloud Drive/操作表中的任何选项后切换回我的应用程序时,我的导航堆栈不会被删除。

    我希望这对将来的人也有帮助。

        2
  •  1
  •   Dheeraj D    7 年前

    请尝试将代码更改为此,然后尝试:

    func share(url: URL, cell : UITableViewCell) {
            documentInteractionController.url = url
            documentInteractionController.uti = url.typeIdentifier ?? "public.data, public.content"
            documentInteractionController.name = url.localizedName ?? url.lastPathComponent
            documentInteractionController.presentOptionsMenu(from: cell.btn.frame, in: view, animated: true)
        }