【问题标题】:open documents directory of my iOS app in Swift在 Swift 中打开我的 iOS 应用程序的文档目录
【发布时间】:2019-10-07 12:37:26
【问题描述】:

在我的 iOS Swift 应用程序中,我通过以下代码在我的应用程序的文档目录中创建了文件:

let localFileName = String("\(fileName).rtf")
let text = String("text text text")

if let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {

    let fileURL = dir.appendingPathComponent(localFileName)
    do {
        try text.write(to: fileURL, atomically: false, encoding: .utf8)
    }
    catch {
    }

}

现在我想在我的应用程序中添加一个指向创建文件的目录的链接,以便用户可以查看文件。目前我正在通过此代码执行此操作:

let importMenu = UIDocumentPickerViewController(documentTypes: [String(kUTTypeRTF)], in: .open)

        importMenu.delegate = self
        importMenu.modalPresentationStyle = .formSheet
        present(importMenu, animated: true, completion: nil)

但是这段代码是用来挑选文件的,不是用来打开目录的,那么我怎样才能打开我的 App 的 Documents 目录,而不是用来挑选文件,只是为了显示文件呢?

【问题讨论】:

  • 你需要创建自己的用户界面来做到这一点

标签: ios swift directory


【解决方案1】:

无法在 iOS 应用中浏览/打开目录。 Apple 没有提供任何 API。您需要自己创建它。

你能做什么

您需要从特定目录中获取所有文件并在表格视图或集合视图中将它们全部列出。

当用户点击任何文件时,您可以在网页视图中显示该文件或根据文件类型执行任何特定操作。

所以最终你需要探索更多关于FileManager的信息。

这个类包含你想要的。

【讨论】:

    猜你喜欢
    • 2018-04-13
    • 1970-01-01
    • 2011-04-18
    • 1970-01-01
    • 1970-01-01
    • 2013-09-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-12
    相关资源
    最近更新 更多