1

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

Francis F
  • 3,157
  • 3
  • 41
  • 79

1 Answers1

5

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

iosdev
  • 90
  • 9
  • 1
    Thanks, your answer helped. I was still able to create the directories with App Sandbox on, by giving permission access to "User Selected File" from Read only to Read/Write. – Francis F Oct 11 '18 at 04:08