您无权在 Mac Mojave 中保存文件

You don't have permission to save file in Mac Mojave

在我的 macOS 应用程序中,我尝试使用以下扩展名创建一个目录

extension URL {
    static func createFolder(folderName: String, folderPath:URL) -> URL? {
        let fileManager = FileManager.default
        let folderURL = folderPath.appendingPathComponent(folderName)
        // If folder URL does not exist, create it
        if !fileManager.fileExists(atPath: folderURL.path) {
            do {
                // Attempt to create folder
                // try fileManager.createDirectory(at: folderURL, withIntermediateDirectories: true, attributes: nil)
                // try fileManager.createDirectory(atPath: folderURL.path, withIntermediateDirectories: true, attributes: nil)
                try fileManager.createDirectory(atPath: folderURL.relativePath, withIntermediateDirectories: true, attributes: nil)
            } catch {
                print(error.localizedDescription + ":\(folderURL.path)")
                return nil
            }
        }
        return folderURL
    }
}

当我调用这个调用时它给我错误

You don’t have permission to save the file “FolderName” in the folder “SelectedFolder”.:/Users/USERNAME/Workspace/SelectedFolder/FolderName

我看过类似的 并尝试了所有方法,但它仍然给我错误,我在这里遗漏了什么吗?感谢任何帮助

我假设你的应用程序是沙盒的。因此,您无权为您尝试写入的位置写入文件夹。

如果它不适用于 Sandboxed,您可以禁用 App Sandbox,它可以通过单击您的项目文件 > 目标名称,选择功能选项卡并关闭 App Sandbox 来关闭。

文件系统编程指南:https://developer.apple.com/library/archive/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/FileSystemOverview/FileSystemOverview.html

App Sandbox 文档在这里:https://developer.apple.com/library/archive/documentation/Security/Conceptual/AppSandboxDesignGuide/AppSandboxInDepth/AppSandboxInDepth.html

您还可以查看用于持久资源访问的安全范围书签。

此处的文档:

https://developer.apple.com/library/archive/documentation/Security/Conceptual/AppSandboxDesignGuide/AppSandboxInDepth/AppSandboxInDepth.html#//apple_ref/doc/uid/TP40011183-CH3-SW16

https://developer.apple.com/library/archive/documentation/Miscellaneous/Reference/EntitlementKeyReference/Chapters/EnablingAppSandbox.html#//apple_ref/doc/uid/TP40011195-CH4-SW18