An easy way to detect user inactivity with very simple usage API.
There are many ways to observe if user is not interact with our app for certain amount of time. Some suggest to subclass UIApplication and override the sendAction method and apply logic from there. Other suggest to subclass UIWindow which is also correct. However, what if we don't want to subclass?
Here's how it work:
- Fire our timer (For this, I used DispatchSource. You can use anything you prefer)
- Observe touches in hittest method of UIWindow and apply logic there
- That's pretty much it!
IdleActivityMonitor.onTimeout {
// do something here
}
IdleActivityMonitor.startMonitoring()
It depends on your usage. You can put it anywhere you want to observe the inactivity. For most use cases, this should be in AppDelegate didFinishLaunchingWithOptions.
Well, that's pretty much it. Now we could find a way to detect if user not touch screen for certain amount of time without subclass UIApplication or UIWindow.