Last active
July 24, 2017 03:17
-
-
Save taktamur/8035158 to your computer and use it in GitHub Desktop.
既存クラスのメソッドを入れ替える(Method Swizzling) ref: http://qiita.com/paming/items/25eaf89e4f448ab05752
This file contains hidden or 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
| - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions | |
| { | |
| // Override point for customization after application launch. | |
| [UIViewController switchLoggingMethod]; // ←これでメソッドが入れ替わる | |
| return YES; | |
| } |
This file contains hidden or 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
| #import <UIKit/UIKit.h> | |
| @interface UIViewController(MethodSwitch) | |
| +(void)switchLoggingMethod; | |
| @end |
This file contains hidden or 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
| #import "UIViewController+MethodSwitch.h" | |
| #import <objc/runtime.h> | |
| @implementation UIViewController(MethodSwitch) | |
| // 入れ替えるメソッドを準備 | |
| -(void)loggingViewDidLoad | |
| { | |
| NSLog(@"%@ viewDidLoad",NSStringFromClass(self.class)); | |
| [self loggingViewDidLoad]; // ここ、無限ループしそうだけど、メソッドの実装が入れ替わるので、元々あったviewDidLoadの実装が呼ばれます。 | |
| } | |
| -(void)loggingViewWillAppear:(BOOL)animated{ | |
| NSLog(@"%@ viewWillAppear",NSStringFromClass(self.class)); | |
| [self loggingViewWillAppear:animated]; | |
| } | |
| -(void)loggingViewDidAppear:(BOOL)animated{ | |
| NSLog(@"%@ viewDidAppear",NSStringFromClass(self.class)); | |
| [self loggingViewDidAppear:animated]; | |
| } | |
| -(void)loggingViewWillDisappear:(BOOL)animated{ | |
| NSLog(@"%@ viewWillDisappear",NSStringFromClass(self.class)); | |
| [self loggingViewWillDisappear:animated]; | |
| } | |
| -(void)loggingViewDidDisappear:(BOOL)animated{ | |
| NSLog(@"%@ viewDidDisappear",NSStringFromClass(self.class)); | |
| [self loggingViewDidDisappear:animated]; | |
| } | |
| +(void)switchLoggingMethod | |
| { | |
| // メソッドを入れ替える | |
| [self switchInstanceMethodFrom:@selector(viewDidLoad) To:@selector(loggingViewDidLoad) ]; | |
| [self switchInstanceMethodFrom:@selector(viewWillAppear:) To:@selector(loggingViewWillAppear:) ]; | |
| [self switchInstanceMethodFrom:@selector(viewDidAppear:) To:@selector(loggingViewDidAppear:) ]; | |
| [self switchInstanceMethodFrom:@selector(viewWillDisappear:) To:@selector(loggingViewWillDisappear:)]; | |
| [self switchInstanceMethodFrom:@selector(viewDidDisappear:) To:@selector(loggingViewDidDisappear:) ]; | |
| } | |
| +(void)switchInstanceMethodFrom:(SEL)from To:(SEL)to | |
| { | |
| // メソッドの入れ替えの実態はここ | |
| Method fromMethod = class_getInstanceMethod(self,from); | |
| Method toMethod = class_getInstanceMethod(self,to ); | |
| method_exchangeImplementations(fromMethod, toMethod); | |
| } | |
| @end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment