Skip to content

Instantly share code, notes, and snippets.

@yutopio
Created November 6, 2014 02:37
Show Gist options
  • Save yutopio/ff175f4c73b8b031b31e to your computer and use it in GitHub Desktop.
Save yutopio/ff175f4c73b8b031b31e to your computer and use it in GitHub Desktop.
Implementation injection
//
// AppDelegate.m
//
// Created by Yuto Takei on 10/8/14.
// Copyright (c) 2014 Yuto Takei. All rights reserved.
//
#import <objc/runtime.h>
#import "AppDelegate.h"
@implementation AppDelegate
IMP originalImp = NULL;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
Class vcClass = [UIViewController class];
SEL sel = @selector(viewDidLoad);
originalImp = class_getMethodImplementation(vcClass, sel);
class_replaceMethod(vcClass, sel, (IMP)implInjected, "v@:");
return YES;
}
void implInjected(id self, SEL _cmd) {
if (self) {
Class c = [self class];
NSLog(@"%@ appear", NSStringFromClass(c));
}
if (originalImp) {
((void (*)(id, SEL))originalImp)(self, _cmd);
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment