Let’s say you have a requirement to boot the user back to the login screen if they sit idle too long. Often this is the case with financial Apps, or anything displaying sensitive information.
This class does basically everything for you. On init() it creates a heartbeat(). Every second it checks if lastActivity + timeoutInterval < now. If true the App has been idle for longer than the timeoutInterval, so it posts a Notification for the App to Log Out.
In your NavigationController (or TabBarController) you’ll listen for this notification.
And in every ViewController add UserActivityTimeout.shared.registerActivity() in viewWillAppear(). You could add it anywhere [scrollViewDidScroll, @IBActions, etc] but I prefer not to clutter up the project. As long as they navigate before 20 minutes they’ll stay logged in.