项目作者: KyriosArk

项目描述 :
A library for complex aspect oriented programming
高级语言: Objective-C
项目地址: git://github.com/KyriosArk/KYHooker.git
创建时间: 2018-03-25T03:25:17Z
项目社区:https://github.com/KyriosArk/KYHooker

开源协议:MIT License

下载


KYHooker

KYHooker allows you adds block of code before/replace/after the selector,and you can manage the block with the identifer,remove or override the code you adds.

KYHooker extends NSObject with the following methods:

  1. /**
  2. Adds a block of code before/replace/after the selector for a specific class with an identifer.
  3. You can remove or override the block of code for a specific hook with the identifer.
  4. @param isInstanceMethod the selector you want to hook is instance method or not
  5. @param block the code you wan to adds,block will return an invocation ,you can get the original method params from invocation
  6. - (void)getArgument:(void *)argumentLocation atIndex:(NSInteger)idx;
  7. @note the first argument of the invocation is the object which calling the method,and the second argument is _cmd
  8. @return Hook success or not
  9. */
  10. + (BOOL)ky_hookSelector:(SEL)selector
  11. isInstanceMethod:(BOOL)isInstanceMethod
  12. position:(KYHookPosition)position
  13. identifier:(NSString *)identifier
  14. withBlock:(void(^ _Nullable )(NSInvocation *invocation))block;
  15. /**
  16. Remove the block of code which added before/replace/after the selector for a specific class with an identifer.
  17. @param isInstanceMethod the selector you want to hook is instance method or not
  18. */
  19. + (void)ky_removeHookForSelector:(SEL)selector
  20. isInstanceMethod:(BOOL)isInstanceMethod
  21. identifier:(NSString *)identifier;

Examples:
here is the method we hooked:

  1. + (void)logMessage:(NSString *)message {
  2. NSLog(@"%@",message);
  3. }

hook the method:

  1. [ViewController ky_hookSelector:@selector(logMessage:) isInstanceMethod:NO position:KYHookPositionAfter identifier:@"1" withBlock:^(NSInvocation *invocation){
  2. NSLog(@"I");
  3. }];
  4. [ViewController ky_hookSelector:@selector(logMessage:) isInstanceMethod:NO position:KYHookPositionAfter identifier:@"2" withBlock:^(NSInvocation *invocation){
  5. NSLog(@"Love");
  6. }];
  7. [ViewController ky_hookSelector:@selector(logMessage:) isInstanceMethod:NO position:KYHookPositionAfter identifier:@"3" withBlock:^(NSInvocation *invocation) {
  8. NSLog(@"Coding");
  9. }];
  10. [ViewController logMessage:@"hello world"];

the logs:

  1. hello world
  2. I
  3. Love
  4. Coding

you can override the block you adds by adding:

  1. [ViewController ky_hookSelector:@selector(logMessage:) isInstanceMethod:NO position:KYHookPositionAfter identifier:@"3" withBlock:^(NSInvocation *invocation) {
  2. NSLog(@"Girls");
  3. }];

the logs:

  1. hello world
  2. I
  3. Love
  4. Girls

and you can remove the block you adds by adding:

  1. [ViewController ky_removeHookForSelector:@selector(logMessage:) isInstanceMethod:NO identifier:@"1"];

the logs:

  1. hello world
  2. Love
  3. Girls