How to reboot from Objective-C?

I want to reboot from an Objective-C program.

What on earth does that mean? You have an app and you want the app to make the iPhone reboot? That's totally impossible.

The dreams and aspirations

What do you want to reboot?

  • The phone? Not Possible
  • The App? Possible but pointless

Hmmm, other folks on this thread have assumed that you’re targeting iOS. Is that the case? If so, then, no, you’re not going to find a solution. OTOH, on macOS you do have some options.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

I want to reboot macOS. I have tried some methods I found with google, but none of them worked.

I want to reboot macOS.

OK, that’s a good start (-:

On macOS there are two types of restart:

  • GUI layer

  • BSD layer

In the GUI case the system closes all the GUI apps, then logs the user out, and then does a BSD layer restart. It’s equivalent to choosing Apple menu > Log Out.

In the BSD case the system just restarts, without telling the closing any apps or logging out the user. It’s kinda like turning the Mac off and then back on again [1].

Which one do you want?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

[1] Not exactly, because low-level subsystems, like the file system, do get a chance to save their state.

The GUI layer log out.

The canonical way to do this is to send a kAERestart Apple event to the system process.

The easiest way to do that is to use NSAppleScript to run the following script:

tell application "System Events"
    restart
end tell

Note that you’ll need:

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Smarter restart AppleScript is here.

tell application "loginwindow" to «event aevtrrst»
How to reboot from Objective-C?
 
 
Q