我们当前的应用程序需要重新启动系统并在单击按钮时干净地退出应用程序,但是一旦其中一个代码运行,即重新启动的代码或退出应用程序的代码,另一个代码将不会运行。我们的应用程序当前在重新启动后重新加载,因为它在系统重新启动之前未正确关闭。 需要重新启动并退出应用程序的按钮代码:
@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)
}
@IBAction func restartAppButton(_ sender: Any) {
if let path = Bundle.main.resourceURL?.deletingLastPathComponent().deletingLastPathComponent().absoluteString {
NSLog("restart (path)")
_ = Process.launchedProcess(launchPath: "/usr/bin/open", arguments: [path])
NSApp.terminate(self)
}
}