1

how could I do that? I want to catch these events and see what's going on in my whole application. Must I subclass UIApplication for that?

UIControl calls this method when an event occurs. It seems like if I subclass UIControl, there won't be a point where I could stick my nose deeper into the details of an event. I can just specify those event mitmasks and call some selector with (id)sender parameter, but with that, I won't see for example the touch coordinates or anything like that.

  • Have you tried adding a category or extension to UIApplication and providing an implementation of -sendAction:to:from:forEvent: ? – Chris Cleeland Aug 07 '09 at 14:10

1 Answers1

2

You can use method swizzling, and provide your own implementation.

@implementaion UIApplication (MyStuff)
- (BOOL)_my_sendAction:(SEL)action to:(id)target from:(id)sender forEvent:(UIEvent *)event
{
    // do your stuff here
    // ...

    // call original selector
    [self _orig_sendAction:action to:target from:sender forEvent:event];
}
@end


BOOL DTRenameSelector(Class _class, SEL _oldSelector, SEL _newSelector)
{
    Method method = nil;

    // First, look for the methods
    method = class_getInstanceMethod(_class, _oldSelector);
    if (method == nil)
        return NO;

    method->method_name = _newSelector;
    return YES;
}

You just need to swap two selectors on application start now:

Class UIApplicationCls = objc_getClass("UIApplication");
DTRenameSelector(UIApplicationCls, @selector(sendAction:to:from:forEvent:), @selector(_orig_sendAction:to:from:forEvent:);
DTRenameSelector(UIApplicationCls, @selector(_my_sendAction:to:from:forEvent:), @selector(sendAction:to:from:forEvent:);
Farcaller
  • 3,070
  • 1
  • 27
  • 42
  • fyi method->method_name no longer compiles – malhal Aug 28 '16 at 21:15
  • I think swizzling is a bad idea for the modern runtime. You still should be able to subclass UIApplication: http://stackoverflow.com/questions/1399202/how-to-subclass-uiapplication – Farcaller Aug 29 '16 at 09:51