How to execute a shell command with root permission from swift
Quick-and-dirty:
NSAppleScript(source: "do shell script \"sudo whatever\" with administrator " +
"privileges")!.executeAndReturnError(nil)
Alternatively look into privileged helper tools but expect that to be much more complicated.
Run a terminal command in a Mac app with Swift Xcode
Try this and let me know if it works. You should do this on a background thread using dispatch_async or NSOperationQueue. For more information on how to do stuff using Grand Central Dispatch read my post: http://manavgabhawala.me/#/blog/grand-central-dispatch
let task = NSTask()
task.launchPath = "/usr/sbin/sysctl"
task.arguments = ["-w", "net.inet.tcp.delayed_ack=0"]
let pipe = NSPipe()
task.standardOutput = pipe
task.standardError = pipe
task.launch()
task.waitUntilExit()
let data = pipe.fileHandleForReading.readDataToEndOfFile()
let output: String = NSString(data: data, encoding: NSUTF8StringEncoding) as! String
Also please can you update the question to include the exact error and the code you are using with NSTask
if this doesn't work. Now this won't have the sudo effect that you wanted if you want that look at: https://developer.apple.com/library/mac/documentation/Security/Conceptual/authorization_concepts/01introduction/introduction.html which tells you about how to use sudo. For an unsandboxed app you could also do the same thing as above but change the launchPath
to /bin/sh
and then add an argument "-S"
and also add the argument "/usr/sbin/sysctl"
. Then you can use standardInput
to pipe in the password of the user who has root access. All password entry errors will come in the standard error pipe. Let me know if this works.
How to run a macOS app as root and use system calls?
You want to run as root
, or you want to run with sudo
? There's a difference. Running as root is definitely not recommended, you will get strange behaviour from the system.
You wrote:
I've tried opening the app with sudo
That should work. How have you tried? You need to call the binary within the .app bundle. Running open
against the bundle won't work.
e.g.
sudo ./Xcode.app/Contents/MacOS/Xcode
How to use Process Class to execute commands need to enter password in MacOS
I found the solution by using "STPrivilegedTask" which is a tool to get authorization by users.
Using FileManager API I think also a good idea
Related Topics
Create View Based Nstableview Programmatically Using Bindings in Swift
What Is the Use of the Validate() Method in Alamofire.Request
Realitykit - How to Set a Modelentity's Transparency
Swift 2.0 'Inout' Function Parameters and Computed Properties
Realitykit as a Framework to Build 3D Nonar Apps
Turn for in Loops Local Variables into Mutable Variables
How to Generate a Binding for Each Array Element
Default Optional Parameter in Swift Function
Lazy Property Initialization in Swift
Using Compiler Variables in Swift
How to Draw a Cosine or Sine Curve in Swift
How to Rotate Sprites Around a Joint
Detect Left and Right Click Events on Nsstatusitem (Swift)
Preventing a Coredata Crash for Upgrading Users
Enumerate Is Unavailable Call the Enumerate Method on the Sequence
Can't Form Range with End < Start Check Range Before Doing for Loop