【问题标题】:Running a command as root in swift application在 swift 应用程序中以 root 身份运行命令
【发布时间】: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


    【解决方案1】:

    创建一个命令行工具并使用sudobash运行它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-22
      • 2011-04-20
      • 2015-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-01
      • 1970-01-01
      相关资源
      最近更新 更多