问题
Currently I am trying to get my hands on Swift programming. I try to execute a command line command from within my macOS Swift application. Without setting my application being sandboxed everything is working fine. But since I activate the Sandbox the command will no longer work but does not throw any errors and terminates with status code 0
.
So the simplest use case is to start VSCode
from my application.
The method which is responsible for executing the shell commands is the following:
func executeUserCommand(command: String, args: [String]?) -> Int32{
let task = Process()
task.launchPath = "/usr/local/bin/" + command
task.arguments = args
task.launch()
task.waitUntilExit()
return task.terminationStatus
}
}
So if I want to start VSCode
I call executeUserCommand("code", ["."])
for opening VSCode
in the current directory.
This works fine until I enable the App Sandbox
in Xcode. I clean and rebuild the application, but now VSCode
does not start at all, but the terminationStatus is 0
. I am pretty sure that the sandbox restricts the access to the /usr/local/bin
directory.
Is there any way of giving my application access to only this specific folder ? So that the user can grant my application access to the folder once, and then the application can access the folder, even after a restart of the application ?
Thanks in advance
回答1:
Yeah, I already thought so, but I was thinking about if it is possible to explicitly ask the user to give the application the permission for this specific folder
You are thinking in the right direction. Clearly sandboxes apps would be pretty useless if they couldn't access files and folders, and the key to them doing so is the user granting them permission. This permission is granted using the standard open and save dialogs, when the user selects a file/folder the app is granted permission to read/write/create it.
Need access to a specific file/folder? Customise the open dialog to guide the user to select that particular file/folder, and check the result to make sure they have - handling the situation when they choose not to.
Wish your app to retain across launches the access right granted by the user to to a file/folder? Then you need to create and store a security scoped bookmark.
Time to do some reading, if you get stuck ask a new question and someone will undoubtedly help you. Remember to show what you've found out, code you've written, etc.
HTH
来源:https://stackoverflow.com/questions/48175937/swift-execute-command-line-command-in-sandbox-mode