简体   繁体   中英

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

In my macOS app I'm trying to create a directory using the below extension

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
    }
}

When I invoke this call its giving me error

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

I have taken look at a similar post and have tried all methods but its still giving me the error, am I missing something here? Any help is appreciated

I am Assuming that your app is sandboxed. So you don't have permission to write folder for location where you are trying to.

If it not intended for Sandboxed you can disable the App Sandbox, it can be turned off by clicking on your project file > target name, selecting the capabilities tab and switching the App Sandbox off.

File System Programming Guide: https://developer.apple.com/library/archive/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/FileSystemOverview/FileSystemOverview.html

App Sandbox documentation here: https://developer.apple.com/library/archive/documentation/Security/Conceptual/AppSandboxDesignGuide/AppSandboxInDepth/AppSandboxInDepth.html

You can also look security scoped bookmark for persistent resource access.

Documentation here:

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

This looks like the folder name isn't set to something sensible

USERNAME seems a bit odd

If you explicitly set it to something real that you know exists what happens?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM