Instant Logging out in objective C

| | August 11, 2015

I want to be able to logout as a security protocol but there is a major issue in that if the user repeatedly taps the keyboard or the mouse the logout will be prevented.

This is my code to logout:

NSTask  *pmsetTask = [[NSTask alloc] init];
pmsetTask.launchPath = @"/usr/bin/pmset";
pmsetTask.arguments = @[@"sleepnow"];
[pmsetTask launch];

2 Responses to “Instant Logging out in objective C”

  1. It won’t be prevented if you just do the simple move and hit this:

    [[(YOURAppDelegate *)[UIApplication sharedApplication].delegate navigationControllerOfRootViewController] popToRootViewControllerAnimated:true];

    this assumes you are using programmatic views and you have declared your navigationControllerOfRootViewController as a navigationController and declared this property in your AppDelegate header file.

    When you call this method, your navigationControllerOfRootViewController kills off everything on top of the stack and HOPEFULLY you have a Splash Screen of some sort that is always set as the navigationControllerOfRootViewController’s rootViewController, that way, when you pop to the bottom of the stack, it just shows your Splash Screen and there’s nothing anyone can do do to stop it. Even better, set animated to “FALSE” and this pop will just happen.

  2. I assume you mean just window sessions? Or do you want ssh sessions as well?

    You have various options. If you’re root, you can use killall loginwindow. That will force everyone to logout of graphics sessions. Similarly, you can killall -m sshd: to drop all SSH sessions.

    As a regular user, I’d probably use killall -m .. That will kill all processes I’m allowed to kill. Of course, if you run that as root, I don’t know if it’ll recover cleanly; I wouldn’t recommend it.

    Be careful with any -m calls to killall, since they can kill more than you mean (it’s regular expression based). But the best way to get to what you’re talking about, especially very fast and without offering the opportunity for the user to prevent it, kill the process that manages the session.

Leave a Reply