Created
April 24, 2021 20:30
-
-
Save dnicolson/7fe244212dd12f34def446878b4ff1ae to your computer and use it in GitHub Desktop.
Read the home directory in a sandboxed app by calling NSOpenPanel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Cocoa | |
import Foundation | |
class ViewController: NSViewController { | |
private func promptForWorkingDirectoryPermission() -> URL? { | |
let openPanel = NSOpenPanel() | |
openPanel.message = "Choose home directory" | |
openPanel.prompt = "Choose" | |
openPanel.allowedFileTypes = ["none"] | |
openPanel.allowsOtherFileTypes = false | |
openPanel.canChooseFiles = false | |
openPanel.canChooseDirectories = true | |
_ = openPanel.runModal() | |
return openPanel.urls.first | |
} | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
let url = promptForWorkingDirectoryPermission() | |
let fileManager = FileManager.default | |
do { | |
let contents = try fileManager.contentsOfDirectory(at: url!, includingPropertiesForKeys: nil) | |
for content in contents { | |
print(content) | |
} | |
} catch { | |
print(error) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment