NSObject+ImpChange.m 870 B

123456789101112131415161718192021
  1. //
  2. // NSObject+ImpChange.m
  3. // assemble
  4. //
  5. // Created by apple on 2020/11/2.
  6. // Copyright © 2020 kingsunsoft. All rights reserved.
  7. //
  8. #import "NSObject+ImpChange.h"
  9. #import <objc/runtime.h>
  10. @implementation NSObject (ImpChange)
  11. + (void)SwizzlingMethod:(NSString *)systemMethodString systemClassString:(NSString *)systemClassString toSafeMethodString:(NSString *)safeMethodString targetClassString:(NSString *)targetClassString{
  12. //获取系统方法IMP
  13. Method sysMethod = class_getInstanceMethod(NSClassFromString(systemClassString), NSSelectorFromString(systemMethodString));
  14. //自定义方法的IMP
  15. Method safeMethod = class_getInstanceMethod(NSClassFromString(targetClassString), NSSelectorFromString(safeMethodString));
  16. //IMP相互交换,方法的实现也就互相交换了
  17. method_exchangeImplementations(safeMethod,sysMethod);
  18. }
  19. @end