DDYHookUtility.m 1.0 KB

123456789101112131415161718192021222324252627282930313233
  1. //
  2. // DDYHookUtility.m
  3. // DDTG
  4. //
  5. // Created by 翟玉磊 on 16/5/6.
  6. // Copyright © 2016年 翟玉磊. All rights reserved.
  7. //
  8. #import "DDYHookUtility.h"
  9. @implementation DDYHookUtility
  10. + (void)SwizzlingInClass:(Class)cls originalSelector:(SEL)originalSelector swizzledSelector:(SEL)swizzledSelector {
  11. Class class = cls;
  12. Method originalMethod = class_getInstanceMethod(class, originalSelector);
  13. Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
  14. BOOL didAddMethod = class_addMethod(class,
  15. originalSelector,
  16. method_getImplementation(swizzledMethod),
  17. method_getTypeEncoding(swizzledMethod));
  18. if (didAddMethod) {
  19. class_replaceMethod(class, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
  20. }else {
  21. method_exchangeImplementations(originalMethod, swizzledMethod);
  22. }
  23. }
  24. @end