Swift 2.1 OSx shell commands using NSTask work when run from xcode, but not when exported

后端 未结 2 1778
萌比男神i
萌比男神i 2021-01-21 18:44

I wrote a simple OSx (10.11) application to execute shell commands when a button is pressed. It works when I run it from xcode, but when I export the application via \"archive\"

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-21 19:05

    Thanks to Eric D. I simplified my code and now everything is working.

    func runCommand(path : String, args : [String]) -> (output: NSString, error: NSString, exitCode: Int32) {
        let task = NSTask()
        task.launchPath = path
        task.arguments = args
    
        let outpipe = NSPipe()
        task.standardOutput = outpipe
        let errpipe = NSPipe()
        task.standardError = errpipe
    
        task.launch()
    
        let outdata = outpipe.fileHandleForReading.readDataToEndOfFile()
        let output = NSString(data: outdata, encoding:  NSUTF8StringEncoding)
    
        let errdata = errpipe.fileHandleForReading.readDataToEndOfFile()
        let error_output = NSString(data: errdata, encoding: NSUTF8StringEncoding)
    
        task.waitUntilExit()
        let status = task.terminationStatus
    
        return (output!, error_output!, status)
    }
    

提交回复
热议问题