【发布时间】:2018-04-09 23:28:12
【问题描述】:
我试图在 Xcode 中实现一个简单的应用程序,它可以让我打开和关闭 wifi 接口。但是,ifconfig 命令需要 root 权限才能运行“up”和“down”。我尝试运行 NSAppleScript 但它返回了
MessageTracer:回退到默认白名单
我也尝试过更改可执行文件的权限,但也没有运气。任何人都可以帮助我以特权或任何其他方式运行命令吗?事先谢谢你(对不起,我是菜鸟)!
这是我的 AppDelegate.swift:
import Cocoa
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
let statusItem = NSStatusBar.system.statusItem(withLength:NSStatusItem.squareLength)
@objc func command(_ sender: Any?) {
let task = Process()
task.launchPath = "/sbin/ifconfig"
task.arguments = ["en0", "down"]
task.launch()
}
func applicationDidFinishLaunching(_ aNotification: Notification) {
if let button = statusItem.button {
button.image = NSImage(named:NSImage.Name("Icon-main"))
button.action = #selector(command(_:))
}
}
func applicationWillTerminate(_ aNotification: Notification) {
// Insert code here to tear down your application
}
}
【问题讨论】:
标签: swift xcode root privileges nstask