2

I was trying to implement a simple app in Xcode that would let me turn on and off a wifi interface. However, the ifconfig command requires root privileges to run "up" and "down". I've tried to run NSAppleScript but it returns

MessageTracer: Falling back to default whitelist

I've also tried changing the privileges of the executable, but without luck either. Could anyone help me out in running the command with privileges or any other way? Thank you beforehand (and sorry, I'm a noob)!

This is my 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
    }
}
Diego Bernal
  • 61
  • 1
  • 6

1 Answers1

-5

Create a command line tool and run it from bash using sudo.

Duncan C
  • 128,072
  • 22
  • 173
  • 272