tags:

views:

160

answers:

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.

A: 

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