Created
July 26, 2016 13:12
-
-
Save verma-manish58/2c77b5c1437841a0b5f3605a8b6a9870 to your computer and use it in GitHub Desktop.
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
// | |
// ViewController+Tracking.h | |
// OCLearning | |
// | |
// Created by Manish Verma on 26/07/16. | |
// Copyright © 2016 Manish Verma. All rights reserved. | |
// | |
#import "ViewController.h" | |
@interface ViewController (Tracking) | |
@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
// | |
// ViewController+Tracking.m | |
// OCLearning | |
// | |
// Created by Manish Verma on 26/07/16. | |
// Copyright © 2016 Manish Verma. All rights reserved. | |
// | |
#import "ViewController+Tracking.h" | |
#import "NSObject+Swizzling.h" | |
@implementation ViewController (Tracking) | |
+ (void)load { | |
[self swizzleInstanceMethodInClass:[self class] | |
originalSelector:@selector(viewWillAppear:) | |
withSwizzledSelector:@selector(mv_ViewWillAppear:)]; | |
} | |
- (void)mv_ViewWillAppear:(BOOL)animated { | |
NSLog(@"View controller tracking category: ViewWillAppear"); | |
[self mv_ViewWillAppear:YES]; | |
} | |
@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
// | |
// NSObject+Swizzling.h | |
// OCLearning | |
// | |
// Created by Manish Verma on 26/07/16. | |
// Copyright © 2016 Manish Verma. All rights reserved. | |
// | |
#import <Foundation/Foundation.h> | |
#import <objc/runtime.h> | |
@interface NSObject (Swizzling) | |
+ (void)swizzleInstanceMethodInClass:(Class)class | |
originalSelector:(SEL)originalSelector | |
withSwizzledSelector:(SEL)swizzledSelector; | |
+ (void)swizzleStaticMethodInClass:(Class)class | |
originalSelector:(SEL)originalSelector | |
withSwizzledSelector:(SEL)swizzledSelector; | |
@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
// | |
// NSObject+Swizzling.m | |
// OCLearning | |
// | |
// Created by Manish Verma on 26/07/16. | |
// Copyright © 2016 Manish Verma. All rights reserved. | |
// | |
#import "NSObject+Swizzling.h" | |
@implementation NSObject (Swizzling) | |
+ (void)swizzleInstanceMethodInClass:(Class)class | |
originalSelector:(SEL)originalSelector | |
withSwizzledSelector:(SEL)swizzledSelector | |
{ | |
static dispatch_once_t onceToken; | |
dispatch_once(&onceToken, ^{ | |
Method originalMethod = class_getInstanceMethod(class, originalSelector); | |
Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector); | |
BOOL didAddedMethod = class_addMethod(class, | |
originalSelector, | |
method_getImplementation(swizzledMethod), | |
method_getTypeEncoding(swizzledMethod)); | |
if (didAddedMethod) { | |
class_replaceMethod(class, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod)); | |
}else{ | |
method_exchangeImplementations(originalMethod, swizzledMethod); | |
} | |
}); | |
} | |
+ (void)swizzleStaticMethodInClass:(Class)class | |
originalSelector:(SEL)originalSelector | |
withSwizzledSelector:(SEL)swizzledSelector | |
{ | |
static dispatch_once_t onceToken; | |
dispatch_once(&onceToken, ^{ | |
Method originalMethod = class_getClassMethod(class, originalSelector); | |
Method swizzledMethod = class_getClassMethod(class, swizzledSelector); | |
BOOL didAddedMethod = class_addMethod(class, | |
originalSelector, | |
method_getImplementation(swizzledMethod), | |
method_getTypeEncoding(swizzledMethod)); | |
if (didAddedMethod) { | |
class_replaceMethod(class, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod)); | |
}else{ | |
method_exchangeImplementations(originalMethod, swizzledMethod); | |
} | |
}); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment