我们当前的应用程序需要重新启动系统并在按钮单击时干净地退出应用程序,但是一旦代码运行,即代码重新启动或代码退出应用程序,其他代码将无法运行。我们的应用程序当前重新加载重启,因为在系统重启之前没有正确关闭。 需要重启并退出app的按钮代码:
@IBAction func exit2(sender: AnyObject) {
let task = NSTask()
let pipe = NSPipe()
task.standardOutput = pipe
//Code to reboot the system
task.launchPath = "/bin/bash/"
task.arguments = ["-c", "osascript -e 'tell app \"System Events\" to restart'"]
let file:NSFileHandle = pipe.fileHandleForReading
task.launch()
task.waitUntilExit()
let data = file.readDataToEndOfFile()
datastring1 = NSString(data: data, encoding: NSUTF8StringEncoding)!
//Code to close the application
NSApplication.sharedApplication().terminate(self)
}