macOS Swift-App write permission problem?

Viewed 649

First I have to say:

  • Sandboxing is off
  • I did give the app full disk access for Mojave
  • I exported it and chose to sign it for manual distribution (without App Store)

Problem is, I try to create a file in /Library/Application support via FileManager.default.createFile what works in my home folders for example /Users/username/Library, so it shouldn't be a programming problem.

But I don't seem to have the permission to write to /Library... How can I grant my app those privileges?

All help is appreciated.

Thanks!

1 Answers

you could try to open a NSOpenPanel and explicitly get the permission for that folder. Here is some code to get you started.

public static func allow(folder: String, prompt: String, callback: @escaping (URL?) -> ())
{
    let openPanel = NSOpenPanel()
    openPanel.directoryURL = URL(string: folder)
    openPanel.allowsMultipleSelection = false
    openPanel.canChooseDirectories = true
    openPanel.canCreateDirectories = false
    openPanel.canChooseFiles = false
    openPanel.prompt = prompt

    openPanel.beginSheetModal(for: self.window!) // use the window from your ViewController
    {
        result in
        if result.rawValue == NSFileHandlingPanelOKButton
        {
            if let url = openPanel.url
            {
                self.store(url: url) // Store for later use.
            }
        }
    }
}

public static func store(url: URL)
{
    guard let path = self.path else { return }

    do
    {
        let data = try url.bookmarkData(options: NSURL.BookmarkCreationOptions.withSecurityScope, includingResourceValuesForKeys: nil, relativeTo: nil)

        NSKeyedArchiver.archiveRootObject(self.folders, toFile: path)
    }
    catch
    {
        Swift.print("Error storing bookmarks")
    }
}
Related