Skip to content

Instantly share code, notes, and snippets.

@dfox
Created August 7, 2012 18:42
Show Gist options
  • Save dfox/3288196 to your computer and use it in GitHub Desktop.
Save dfox/3288196 to your computer and use it in GitHub Desktop.
Method Swizzling in UIApplication to Capture Touch Events
#import "UIApplication+EventInterceptor.h"
#import <objc/runtime.h>
#import "EventLogger.h"
@implementation UIApplication (EventInterceptor)
+(void) load
{
//Swap the implementations of our interceptor and the original sendEvent:
Method oldMethod = class_getInstanceMethod(self, @selector(sendEvent:));
Method newMethod = class_getInstanceMethod(self, @selector(interceptAndSendEvent:));
method_exchangeImplementations(oldMethod, newMethod);
}
-(void) interceptAndSendEvent: (UIEvent *) event
{
for (UITouch *touch in event.allTouches){
if (touch.phase == UITouchPhaseBegan){
[EventLogger logEvent:EVENT_LOGGER_TOUCHED forObject:touch.view];
}
}
[self interceptAndSendEvent:event];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment