4

I'm looking to make an iphone that will log a user out of a session once the app has been either idle in the foreground or in the background for a specified period of time.

I'm guessing that I should use an NSTimer, but I don't know where to start it. How can I tell when a user has been idle when the app is in the foreground? I'm guessing that it should be a UIApplicationDelegate method?

Any suggestions? Thanks.

Cyrus
  • 3,687
  • 5
  • 35
  • 67

1 Answers1

2

If you want to auto logout when the application enters background, try looking at the application delegate methods. For example, there is a applicationDidEnterBackground method, and applicationWillTerminate could also be helpful. This is where you could implement your automatic logout. If you want to logout after the application is in the background for a certain period of time, then you need to enable your application to run in the background. To do that just go to the application info.plist file and add a new field "Application does not run in background". Make sure it is UNchecked. Then in the applicationDidEnterBackground method add an NSTimer, and after a certain period of time you can logout.

futurevilla216
  • 982
  • 3
  • 13
  • 29
  • Thanks. That's really helpful for the background logout. Any thoughts on how to make the app logout when it's been idle in the foreground for a certain period of time. Say the user hasn't touched the screen for 15 minutes (ie left the iphone out) ... – Cyrus Mar 31 '11 at 23:47
  • 1
    Hmmm...I'm not sure about that one...look at this question, it might help: http://stackoverflow.com/questions/273450/iphone-detecting-user-inactivity-idle-time-since-last-screen-touch – futurevilla216 Apr 01 '11 at 00:01
  • 2
    @lenny-k Background processing on iOS is limited to a very few select tasks (e.g. VOIP or streaming audio). You can't just set up an NSTimer before being backgrounded... A more workable strategy is to see how much time has passed since the app was backgrounded (at foregrounding time), and if greater than a certain amount, do the logout action. Discussion about background timers: http://www.iphonedevsdk.com/forum/iphone-sdk-development/58643-keep-nstimer-running-when-app-background-multitasking.html – occulus Apr 01 '11 at 01:40