-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented method swizzling and included examples
- Loading branch information
1 parent
803bdf7
commit 2895c7c
Showing
11 changed files
with
180 additions
and
40 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// | ||
// ASHook+MethodSwizzler.h | ||
// ASHookIt | ||
// | ||
// Created by Adam Szedelyi on 2017. 02. 26.. | ||
// Copyright © 2017. Adam Szedelyi. All rights reserved. | ||
// | ||
|
||
#import "ASHook.h" | ||
|
||
@interface ASHook (MethodSwizzler) | ||
|
||
/** | ||
* Replaces the implementations of the two given selectors of an instance. | ||
* | ||
* @param targetInstance The instance where you want to replace selectors. | ||
* @param originalSelector The original selector that is to be replaced. | ||
* @param newSelector The new selector will replace the original selector's implementation. | ||
* | ||
*/ | ||
+ (void)swizzle:(id)targetInstance instanceSelector:(SEL)originalSelector withInstanceSelector:(SEL)newSelector; | ||
|
||
/** | ||
* Replaces the implementations of the two given selectors of a class. | ||
* | ||
* @param targetClass The class where you want to replace selectors. | ||
* @param originalSelector The original selector that is to be replaced. | ||
* @param newSelector The new selector will replace the original selector's implementation. | ||
* | ||
*/ | ||
+ (void)swizzle:(id)targetClass classSelector:(SEL)originalSelector withClassSelector:(SEL)newSelector; | ||
|
||
@end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// | ||
// ASHook+MethodSwizzler.m | ||
// ASHookIt | ||
// | ||
// Created by Adam Szedelyi on 2017. 02. 26.. | ||
// Copyright © 2017. Adam Szedelyi. All rights reserved. | ||
// | ||
|
||
#import "ASHook+MethodSwizzler.h" | ||
#import <objc/runtime.h> | ||
#import <objc/message.h> | ||
|
||
@implementation ASHook (MethodSwizzler) | ||
|
||
#pragma mark - Public methods | ||
|
||
+ (void)swizzle:(id)targetInstance instanceSelector:(SEL)originalSelector withInstanceSelector:(SEL)newSelector { | ||
id target = object_getClass(targetInstance); | ||
Method originalMethod = class_getInstanceMethod(target, originalSelector); | ||
Method newMethod = class_getInstanceMethod(target, newSelector); | ||
[self swizzle:target selector:originalSelector newSelector:newSelector originalMethod:originalMethod newMethod:newMethod]; | ||
} | ||
|
||
+ (void)swizzle:(id)targetClass classSelector:(SEL)originalSelector withClassSelector:(SEL)newSelector { | ||
id target = object_getClass(targetClass); | ||
Method originalMethod = class_getClassMethod(target, originalSelector); | ||
Method newMethod = class_getClassMethod(target, newSelector); | ||
[self swizzle:target selector:originalSelector newSelector:newSelector originalMethod:originalMethod newMethod:newMethod]; | ||
} | ||
|
||
#pragma mark - Private methods | ||
|
||
+ (void)swizzle:(id)target | ||
selector:(SEL)originalSelector | ||
newSelector:(SEL)newSelector | ||
originalMethod:(Method)originalMethod | ||
newMethod:(Method)newMethod { | ||
BOOL methodAdded = class_addMethod(target, originalSelector, method_getImplementation(newMethod), method_getTypeEncoding(newMethod)); | ||
if (methodAdded) { | ||
class_replaceMethod(target, newSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod)); | ||
} else { | ||
method_exchangeImplementations(originalMethod, newMethod); | ||
} | ||
} | ||
|
||
@end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// | ||
// ASHook.h | ||
// ASHookIt | ||
// | ||
// Created by Adam Szedelyi on 2017. 02. 26.. | ||
// Copyright © 2017. Adam Szedelyi. All rights reserved. | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
@interface ASHook : NSObject | ||
|
||
@end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// | ||
// ASHook.m | ||
// ASHookIt | ||
// | ||
// Created by Adam Szedelyi on 2017. 02. 26.. | ||
// Copyright © 2017. Adam Szedelyi. All rights reserved. | ||
// | ||
|
||
#import "ASHook.h" | ||
|
||
@implementation ASHook | ||
|
||
@end |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,5 @@ | |
|
||
@property (strong, nonatomic) UIWindow *window; | ||
|
||
|
||
@end | ||
|
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
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
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