【问题标题】:Save file using Swift and iOS使用 Swift 和 iOS 保存文件
【发布时间】:2020-01-11 13:33:38
【问题描述】:

我有一个 iOS 应用程序,我需要能够在我的 iCloud Drive 上选择一个文件,修改该文件,并使用新的扩展名保存修改后的文件。我已经尝试了很多东西,但我仍然无法编写新文件。这是我最新的代码:

documentPickerViewController = DocumentPickerViewController(documentTypes: ["public.item"], in: .open)  
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {  
    let fileURL = urls[0]  
    do {  
        // Read the file       
        _ = fileURL.startAccessingSecurityScopedResource()  
        let fileData = try Data(contentsOf: fileURL, options: .uncached)  
        fileURL.stopAccessingSecurityScopedResource()  

        // write the file  
        let fileCopyURL = fileURL.appendingPathExtension("copy")  
        _ = fileCopyURL.startAccessingSecurityScopedResource()  
        try fileData.write(to: fileCopyURL)  
        fileCopyURL.stopAccessingSecurityScopedResource()  
    }  
    catch {  
        print(error.localizedDescription)  
    }  
}  

当我在 iCloud Drive 上选择文件时,我收到以下错误:

您无权将“TestFile.txt.copy”文件保存在“Test Files”文件夹中。

如何保存修改后的文件?

【问题讨论】:

    标签: ios swift icloud-drive


    【解决方案1】:

    首先,我认为您应该确保您拥有正确的权利/权限来写入 iCloud Drive:

    如果这仍然不起作用,请尝试更新您的应用程序的 CFBundleVersion 或内部版本号,并确保您的 Bundle Identifier(在 Signing & Capabilities 的第一部分中)已正确注册到您的 Apple 开发者帐户中。

    根据您的代码,您尝试完成的工作是可行的,但问题来自其他地方。

    如果您在执行这些步骤后遇到问题,请告诉我。祝你好运!

    【讨论】:

    • 感谢您的建议,但很遗憾他们没有提供帮助。
    • @JeffZacharias 根据您的代码,您正在尝试复制一个文件,我不确定这是否可行(至少您这样做的方式)。尝试使用copyItemAtPath
    • 我已经尝试过了,但我得到了同样的错误。好像我读到当您使用 UIDocumentPickerViewController 或 UIDocumentBroswerPicker 时,您只能访问选择的文件,但我找不到我在哪里读到的。
    • 那么我必须说实话:我不知道出了什么问题。对不起?。 @JeffZacharias
    猜你喜欢
    • 2021-09-05
    • 2015-06-11
    • 1970-01-01
    • 2023-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-16
    • 1970-01-01
    相关资源
    最近更新 更多