Is it possible to intercept all user actions like tap, swipe, enter text, etc. on all windows of my app?
Asked
Active
Viewed 621 times
1
-
add an UIView with clear color background and full size of screen as the first view – Cong Tran Dec 25 '15 at 02:58
-
Wouldn't it override all underlying elements? Is there an interceptor-style code to do it? – Artem Dec 25 '15 at 03:02
-
2A cleaner approach would be to subclass `UIApplication` and override `sendEvent`. – Nicolas Miari Dec 25 '15 at 03:08
-
Can I dynamically override sendEvent() at runtime? – Artem Dec 25 '15 at 03:14
-
@Artem search for "Objective-C method swizzling" – Nicolas Miari Dec 25 '15 at 04:07
-
1Swipe it is not atomic action, it is gesture, that contains many touches. Also with other "actions". I think, more clear solution is create custom UIViewControllerClass, who will be listener for all actions and events, what you want. For example, if you want be notified about any touches and gestures, you can add UITapGestureRecognizer to the controller's view; – Andrew Romanov Dec 25 '15 at 05:37
1 Answers
3
Like I said in the comments, subclass UIApplication
and override the instance method sendEvent:
.
From the documentation for the UIApplication class, -sendEvent:
method:
Discussion
If you require it, you can intercept incoming events by subclassing UIApplication and overriding this method. For every event you intercept, you must dispatch it by calling [super sendEvent:event] after handling the event in your implementation.
So, it would look like this:
CustomUIApplication.h:
@interface CustomUIApplication:UIApplication
- (void)sendEvent:(UIEvent *)event;
@end
CustomUIApplication.m:
@implementation CustomUIApplication
- (void)sendEvent:(UIEvent *)event
{
// ...Do your thing...
[super sendEvent:event];
}
@end
Of course, you need to make sure your subclass is used instead of the default UIApplication
. Here is a Stack Overflow answer on how to do it in Objective-C, and here in Swift.

Community
- 1
- 1

Nicolas Miari
- 16,006
- 8
- 81
- 189