zoukankan      html  css  js  c++  java
  • Hook~iOS用钩子实现代码注入(埋点方案)

    想要为某各类添加钩子首先要建立这个类或父类的分类,运用runtime的方法交换的方法实现交换再调回原方法 这就是钩子的基本思路  运用lldb 查看方法的调用堆栈 就可以找到在这个方法之前调用的方法,然后我们拦截它,交换它!

    lldb 的命令 thread backtrace  查看调用堆栈

    找到你需要的拦截的方法

        Method applicationLanch = class_getInstanceMethod([self class],@selector(application: didFinishLaunchingWithOptions:));

        Method newApplicationLanch = class_getInstanceMethod([self class], @selector(configurationApplication: didFinishLaunchingWithOptions:));

        method_exchangeImplementations(applicationLanch, newApplicationLanch);

    然后调回本身:

    - (BOOL)configurationApplication:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

        BOOL result = [self configurationApplication:application didFinishLaunchingWithOptions:launchOptions];

        NSLog(@"%@AppDelegate+Hook",

              [self class]);

        return result;

    }

    注意这两个方法已经交换所以实际调用的是application: didFinishLaunchingWithOptions: 这个方法,那么代理方法怎么拦截呢?

    也是一样的只要拦截 交换setDelegate: 方法 在交换的方法中遵守代理 然后在这个方法里继续交换你想要交换的方法

    #define GET_CLASS_CUSTOM_SEL(sel,class)  NSSelectorFromString([NSString stringWithFormat:@"%@_%@",NSStringFromClass(class),NSStringFromSelector(sel)])

    - (void)fd_setDelegate:(id<UIScrollViewDelegate>)delegate {

        if ([self isMemberOfClass:[UIScrollView class]]) {

            if (![self isContainSel:GET_CLASS_CUSTOM_SEL(@selector(scrollViewWillBeginDragging:),[delegate class]) inClass:[delegate class]]) {

                [self swizzling_scrollViewWillBeginDragging:delegate];

            }

            [self fd_setDelegate:delegate];

        }

    }

    - (BOOL)isContainSel:(SEL)sel inClass:(Class)class {

        unsigned int count;

        

        Method *methodList = class_copyMethodList(class,&count);

        for (int i = 0; i < count; i++) {

            Method method = methodList[i];

            NSString *tempMethodString = [NSString stringWithUTF8String:sel_getName(method_getName(method))];

            if ([tempMethodString isEqualToString:NSStringFromSelector(sel)]) {

                return YES;

            }

        }

        return NO;

    }

    挥毫泼墨,书写人生篇章
  • 相关阅读:
    013.ES6 -对象字面量增强型写法
    012. ES6
    011. ES6 语法
    10. 9. Vue 计算属性的setter和getter 以及 计算属性的缓存讲解
    4. Spring MVC 数据响应方式
    3. SpringMVC 组件解析
    9. Vue 计算属性
    【洛谷 2984】给巧克力
    【洛谷 1821】捉迷藏 Hide and Seek
    【洛谷 1821】银牛派对Silver Cow Party
  • 原文地址:https://www.cnblogs.com/Jusive/p/6943709.html
Copyright © 2011-2022 走看看