Trigger an action on Power Off

G'day !


Like the title said, I wanna trigger an action when the shut down button is pressed.


My goal is to display a password screen before seeing the "slide to power off" screen.


How could I do that ?


Thank you,

DaVinCode

I presume you’re talking about iOS here, in which case the answer is that this is not possible.

Normally I’d suggest you file an enhancement request describing your requirements but in this case I think it’s very unlikely that it would be successful. iOS apps are not, in general, allow to alter the behaviour of the system as a whole. Features like this are usually implemented in the system itself, accessible the user interface (Settings > General > Restrictions) or a configuration profile.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Thank you for taking the time and having replied my post Eskimo!


Could I create a password view in my app and trigger it on shut down/turn off that would open my app then display the password view and then ask for the right password previously set up in my app ?


Thanks a lot!

DaVinCode

That is the same question as your initial post, hence, same reply.


But I don't understand the logic of what you want to do.


Shut down is shut down, not launching back an app.


Asking for password is something you will do when launching your app.

My goal is to trigger a password screen before shuting down the phone.


Any other way I could do that feature ?

You cannot stop the shutdown process.


You want a password for your app ? Why do you need at this time of shutdown ? Can't you ask the password before, at launch or for first modification of data ?


Or in

func applicationDidEnterBackground(_ application: UIApplication) { }


You can do actions in applicationWillTerminate, but not ask an alert

    func applicationWillTerminate(_ application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
        print("I terminate")
    }

As noted, your app cannot grab control of a shutdown as you require.



See this doc for details on various states and how your app is expected to react to them them:


https://developer.apple.com/library/archive/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/StrategiesforHandlingAppStateTransitions/StrategiesforHandlingAppStateTransitions.html

That's it ! I want a password BEFORE shutting down the phone : )


The goal here is to prevent thiefs to turn off your phone if they don't have the right password.


So "applicationWillTerminate" will let me trigger any actions I want to perform whenever I remove/terminate the app, right ?


Do you know if it's going to be triggered before the shuting down process ?

But you should do it by system settings (I think it is now in parental control), not by your app.


have a look here


https://support.apple.com/en-gb/HT201304

The goal here is to prevent thiefs to turn off your phone if they don't have the right password.

Other folks have answered here but I want to recap. iOS, in general, does not allow apps to affect the behaviour of the system as a whole, and powering off the device is one such behaviour. If there were to be such a feature, it would have to be implemented in the OS, not as part of an app.

If you’d like the system to support this, my recommendation is that you file an enhancement request along those lines.

You can also file an ER requesting an API for your app to get involved in this process, but in my opinion it’s very unlikely that such a mechanism would be implemented. OTOH, it never hurts to ask (-:

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
Trigger an action on Power Off
 
 
Q