使用Swift执行'killall'时不允许进行操作 [英] Operation not permitted when executing 'killall' with Swift

查看:155
本文介绍了使用Swift执行'killall'时不允许进行操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个菜单栏应用程序,以隐藏桌面图标以及希望其他各种东西,主要是为了更多地了解Swift,由于某种原因,我无法使其正常工作.当我运行该程序并单击菜单项之一时,什么也没有发生,并且我在控制台中收到此警告:

I'm trying to create a menu bar app to hide desktop icons and hopefully various other things mostly to learn more about Swift, and for some reason I can't get it to work. When I run this program and click on one of the menu items, nothing happens and I get this warning in the console:

killall: warning: kill -TERM 15175: Operation not permitted

其他命令也可以使用,但是我尝试使用"killall"的任何变体都会吐出类似上面的内容.目前,我的代码如下:

Other commands work, but any variant I try on "killall" spits out something like the above. Currently my code looks like this:

@discardableResult
func killStuff(_ args: String...) -> Int32 {
    let task = Process()
    let pipe = Pipe()

    task.launchPath = "/usr/bin/killall"
    task.arguments = args
    task.standardOutput = pipe
    task.standardError = pipe
    task.launch()
    task.waitUntilExit()

    let data = pipe.fileHandleForReading.readDataToEndOfFile()
    if let output = String(data: data, encoding: .utf8) {
        print(output)
    }

    return task.terminationStatus
}

我对找到的可接受解决方案进行了多种尝试,此处以及我已在Google上找到,但我一直收到相同的不允许操作"的信息.当我在Xcode游乐场中运行相同的代码时,它就可以正常工作.

I've tried numerous variations on the accepted solutions found here, here, and what I've found on Google, but I keep getting the same "Operation not permitted." When I run the same code in an Xcode playground, it works just fine.

提前谢谢!

推荐答案

Mac应用程序(如iOS应用程序)默认为沙箱,这意味着它们对系统资源的访问非常有限.例如,仅允许它们读取和写入少量沙盒目录,并且绝对不允许从usr/bin读取/写入/执行,也不允许您启动进程.

Mac apps, like iOS apps, are sandboxed by default, which means they have very limited access to system resources. They are only allowed to read and write to a small number of sandboxed directories, for example, and read/write/execute from usr/bin is most definitely not allowed, and nor will you be allowed to launch processes.

我记得,通过删除您的权利文件,您正在构建未沙盒化的应用程序.您可以从Xcode运行它,但是需要更改系统设置才能从查找程序运行它.

As I recall, by deleting your entitlements file you are building an app that isn't sandboxed. You can run it from Xcode, but will need to change your system settings in order to be able to run it from the finder.

这篇关于使用Swift执行'killall'时不允许进行操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆