How to check if user touched anything in your ios app

So you want to know when user is interacting with your app and you don’t really care which screen is he using or what is he doing.

You only want to know when he touches your app, for example: you make a game and you would like to automatically go into pause mode if user didn’t use your app for N minutes…

Implement a simple solution

Doing that at each top view level would be waste of time and not something that you really need.

You could add some custom subview to your window, intercept touches there and try to somehow pass it along to real views that needs it ( after all you can have many interactive views ). But this is not simple, it requires subclassing, adding custom views to windows…

Actually UIWindow is subclass of UIView, that means you can add a gesture recognizer to it. But how to make sure this will not have influence on real content of your app ?

Make AppDelegate the delegate of UIGestureRecognizer that you just added, and override shouldReceiveTouch as follows:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
  NSLog(@"touch at %@", NSStringFromCGPoint([touch locationInView:touch.view]));
  return NO;
}

This allows you to intercept any screen touches but ignores it and prevents your code from influencing your app normal behavior ( UIControls or other UIGestureRecognizers works as always ) .

Conclusion

I've shown you a simple way to always know when and where user puts his finger, without interfering with other interactions in your app.

You've successfully subscribed to Krzysztof Zabłocki
Great! Next, complete checkout to get full access to all premium content.
Error! Could not sign up. invalid link.
Welcome back! You've successfully signed in.
Error! Could not sign in. Please try again.
Success! Your account is fully activated, you now have access to all content.
Error! Stripe checkout failed.
Success! Your billing info is updated.
Error! Billing info update failed.