Created
November 6, 2014 02:37
-
-
Save yutopio/ff175f4c73b8b031b31e to your computer and use it in GitHub Desktop.
Implementation injection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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