1

Is there a way to run an action when the iPhone gets locked?

I simply want to run a little bit of code when the iPhone gets locked by the user. I'm looking for a solution that works without a jailbreaked iPhone.

Emil
  • 7,220
  • 17
  • 76
  • 135

2 Answers2

3

https://stackoverflow.com/a/7888507/267892

May be you need to implement following methods in AppDelegate:

Tells the delegate that the application is now in the background.

- (void)applicationDidEnterBackground:(UIApplication *)application

Tells the delegate that the application has become active.

- (void)applicationDidBecomeActive:(UIApplication *)application

Tells the delegate that the application is about to become inactive.

- (void)applicationWillResignActive:(UIApplication *)application

See UIApplicationDelegate Protocol Reference for more info on these.

Community
  • 1
  • 1
Emil
  • 7,220
  • 17
  • 76
  • 135
  • Does that even work if the app is running in background? (sorry, i´m new to iOs-programming and i´m often not sure whar i´ve to google.. ) –  Feb 10 '12 at 20:12
  • Unfortunately not, the only thing you can do is get notified whenever the user closes your app. – Emil Feb 10 '12 at 20:24
  • hm, so there is no possibility to run code when the iphone gets locked and the app is in background? Thanks for your fast answer. –  Feb 10 '12 at 20:29
  • As far as my knowledge goes, no. Background-tasks and notifications are limited to what is mentioned in the "App States and Multitasking" in Apple's iOS App Programming Guide: https://developer.apple.com/library/IOs/#documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html – Emil Feb 10 '12 at 20:33
2

Take a look at UIApplicationDelegate Protocol Reference

especially applicationProtectedDataDidBecomeAvailable: and applicationProtectedDataWillBecomeUnavailable:

Rok Jarc
  • 18,765
  • 9
  • 69
  • 124