如何在SwiftUI中位于Apple的Files应用程序的文件夹中写入文件?

沉思

我按照Swift项目UsingFiles github及其视频在SwiftUI项目中写入文件。项目UsingFiles可以将文件写入Files APP,然后可以在Files APP中看到写入的文件。但是我遵循以下代码,无法在Files APP中看到文件。

let file = "\(UUID().uuidString).txt"
let contents = "Some text..."

let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let fileURL = dir.appendingPathComponent(file)

do {
    try contents.write(to: fileURL, atomically: false, encoding: .utf8)
}
catch {
    print("Error: \(error)")
}

UsingFiles写入文件为file:///Users/fmac/Library/Developer/CoreSimulator/Devices/11111111-881E-488A-9571-E61B83EB6062/data/Containers/Data/Application/11111111-AF89-4B15-B3B5-E13A63A19F8D/Documents/E018F056-83EA-4D70-87C4-16F755AA404A.txt

我的写作文件是 file:///Users/fmac/Library/Developer/CoreSimulator/Devices/11111111-881E-488A-9571-E61B83EB6062/data/Containers/Data/Application/11111111-B191-4ACD-98B1-004E619C2EC7/Documents/C9E0F52E-040F-4647-94A3-88E0DA171AB5.txt

我可以在目录中找到UsingFiles的写入文件,UsingFiles如下所示:在此处输入图片说明

但是我在SwiftUI项目的Files APP中找不到写入文件。SwiftUI中的代码有问题吗?为什么在Files APP中找不到写入文件?

里奥·达布斯

正如我已经在评论中发布的那样,您不能以编程方式将文件保存到APP捆绑包之外。您可以使用UIDocumentInteractionController并要求用户选择应该将文件写入的位置。

所以,如果你与SwiftUI这个工作变得比普通故事板的方法有点复杂,你可以在此看到帖子,因为你需要实现UIViewControllerRepresentableUIDocumentInteractionController


struct DocumentInteractionController: UIViewControllerRepresentable {

    fileprivate var isExportingDocument: Binding<Bool>
    fileprivate let viewController = UIViewController()
    fileprivate let documentInteractionController: UIDocumentInteractionController

    init(_ isExportingDocument: Binding<Bool>, url: URL) {
        self.isExportingDocument = isExportingDocument
        documentInteractionController = .init(url: url)
    }

    func makeUIViewController(context: UIViewControllerRepresentableContext<DocumentInteractionController>) -> UIViewController { viewController }

    func updateUIViewController(_ controller: UIViewController, context: UIViewControllerRepresentableContext<DocumentInteractionController>) {
        if isExportingDocument.wrappedValue && documentInteractionController.delegate == nil {
            documentInteractionController.uti = documentInteractionController.url?.typeIdentifier ?? "public.data, public.content"
            documentInteractionController.name = documentInteractionController.url?.localizedName
            documentInteractionController.presentOptionsMenu(from: controller.view.frame, in: controller.view, animated: true)
            documentInteractionController.delegate = context.coordinator
            documentInteractionController.presentPreview(animated: true)
        }
    }

    func makeCoordinator() -> Coordintor { .init(self) }
}

及其协调人:

class Coordintor: NSObject, UIDocumentInteractionControllerDelegate {
    let documentInteractionController: DocumentInteractionController
    init(_ controller: DocumentInteractionController) {
        documentInteractionController = controller
    }
    func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController { documentInteractionController.viewController }

    func documentInteractionControllerDidDismissOptionsMenu(_ controller: UIDocumentInteractionController) {
        controller.delegate = nil
        documentInteractionController.isExportingDocument.wrappedValue = false
    }
}

现在,您可以创建DocumentInteraction视图及其预览:

struct DocumentInteraction: View {
    @State private var isExportingDocument = false
    var body: some View {
        VStack {
            Button("Export Document") { self.isExportingDocument = true }
            .background(DocumentInteractionController($isExportingDocument,
                url: Bundle.main.url(forResource: "sample", withExtension: "pdf")!))
        }
    }
}

struct DocumentInteraction_Previews: PreviewProvider {
    static var previews: some View { DocumentInteraction() }
}

您还将需要那些帮助者:

extension URL {
    var typeIdentifier: String? { (try? resourceValues(forKeys: [.typeIdentifierKey]))?.typeIdentifier }
    var localizedName: String? { (try? resourceValues(forKeys: [.localizedNameKey]))?.localizedName }
}

样例项目

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在Swift中将文件写入位于Apple的Files应用程序的文件夹中

应用程序如何在 Documents 文件夹中写入和读取文件?

如何写入位于程序文件 (x86) 安装文件夹 winform 应用程序中的 json 文件?

如何在Viewpager应用程序中全屏显示位于可绘制文件夹中的图像?

如何在GNOME应用程序菜单中创建应用程序文件夹?

如何在React应用程序中从公用文件夹导入文件?

如何在Codeigniter应用程序的文件夹中访问CSS文件?

Rails 5:如何在应用程序模板脚本中添加文件夹和文件

如何在UWP应用程序的AppX中包含文件夹和文件?

如何在Web应用程序的文件夹中的网页中填充下拉列表?

ACRA 4.9.0:如何将ACRA报告写入文件(在“应用程序数据”文件夹中)

写入应用程序.app文件夹中的文件是一种不好的做法吗?

如何在32位应用程序中获取64位系统文件夹?

如何在UWP应用程序中创建文件夹?

如何在Java中获取本地应用程序数据文件夹?

如何在 NativeScript 应用程序中忽略 Webpack 处理的文件夹

如何在Kubernetes中为Web应用程序创建上传文件夹?

如何在VBScript中获取某个IIS应用程序/站点的日志文件夹路径

PHP:如何在.htaccess中隐藏我的应用程序文件夹

如何在特定文件夹中启动 Windows 10 Mail 应用程序?

如何在Android应用程序中创建隐藏文件夹?

如何在libgdx的核心或桌面应用程序中添加资源文件夹?

如何在Android Gallery中创建应用程序特定的文件夹?

如何在本地git文件夹中删除对旧的Heroku应用程序的引用?

如何在 Yii2 Advanced 中添加新的应用程序文件夹

如何在 Rails 应用程序中配置虚拟文件夹?

应用扩展程序如何访问包含应用程序文档/文件夹中的文件

打开应用程序文件夹中的文件

写入沙盒应用程序中的下载文件夹