Search code examples
iosswiftforegroundbackground-foreground

How to check if an app is in foreground but not accessed for a specific time - iOS


I am developing an app in Swift 2.0 that uses http requests. And I am passing a checksum value to the requests. This checksum is generated by the web server once a user logs in. The checksum value is refreshed once it is not used for 3 hours. Because of the change in checksum value, I am facing an issue. The problem is, the session gets expired and the user needs to login again to use the app.

When the app enters background(minimized) and re-opened, I am re-generating the checksum in the applicationDidBecomeActive method. I use applicationDidEnterBackground to note down the time. If the noted time is more than 3 hours, then I am re-generating the checksum value. And it is working fine in this case.

The problem occurs when the app is in foreground and it is not accessed (the app is ideal) for more than 3 hours.

Where should I re-generate the checksum in this case? Is there any AppDelegate methods available for this?


Solution

  • There is no method in view life cycle which called if application is in foreground mode for longest period of time without navigating other page . so setting timer could solve your problem and prompt user with "Session Expired " alert and when user tap on it , just hit web API and refresh UI Accordingly .

    Thanks.