问题描述
目前,我正在尝试着手Swift编程。我尝试从我的 macOS Swift应用程序内部执行命令行命令。没有设置我的应用程序为沙盒,一切都可以正常工作。但是,由于我激活了沙箱,因此该命令将不再起作用,但不会引发任何错误,并以状态代码 0
终止。
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
.
所以最简单的用例是从应用程序中启动 VSCode
。
So the simplest use case is to start VSCode
from my application.
负责执行shell命令的方法如下:
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
}
}
因此,如果我想启动 VSCode
,我会调用 executeUserCommand( code,[。])
用于在当前目录中打开 VSCode
。
So if I want to start VSCode
I call executeUserCommand("code", ["."])
for opening VSCode
in the current directory.
在我启用Xcode中的 App沙箱
之前,此方法正常工作。我清理并重建了该应用程序,但现在 VSCode
根本无法启动,但是终止状态为 0
。我很确定沙箱会限制对 / usr / local / bin
目录的访问。
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 ?
在此先感谢
推荐答案
您正在考虑正确的方向。显然,如果沙箱应用无法访问文件和文件夹,它们将毫无用处,而这样做的关键是用户向其授予权限。使用标准的打开和保存对话框可以授予此权限,当用户选择文件/文件夹时,将授予该应用读取/写入/创建该应用的权限。
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
这篇关于沙盒模式下的Swift Execute命令行命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!