Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save chunta/cf606ee9bccdb1ba68b71a23f1e954e7 to your computer and use it in GitHub Desktop.
Save chunta/cf606ee9bccdb1ba68b71a23f1e954e7 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