iOS 高级hookiOS OC

iOS 不常用技术解密之 hook

2017-08-12  本文已影响177人  wnido

在开发中,hook 技术虽然不常用,但在一些特定情况下有非常大的作用,另外了解下 hook 的原理,也可防止被 hook 改写,本篇就讲述下 hook 的基本用法。

原理

hook 其实就是处理消息的程序段,每当消息发出,在没有到达特定的程序或者窗口前,hook 程序会先将此消息截获,也就是先得到控制权,然后 hook 就可以对此段消息做出处理,查看或修改其内容,甚至更改其传递路线。

常用 hook


method swizzle

正常开发中,使用的场景也不是很多,但在日志统计方面,可以避免使用继承的方式,使用少量代码实现

下面举个例子

对 viewController 页面行为进行统计

核心代码

void __gbh_tracer_swizzleMethod(Class class, SEL originalSelector, SEL swizzledSelector){
    Method originalMethod = class_getInstanceMethod(class, originalSelector);
    Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
    
    BOOL didAddMethod =
    class_addMethod(class,
                    originalSelector,
                    method_getImplementation(swizzledMethod),
                    method_getTypeEncoding(swizzledMethod));
    
    if (didAddMethod) {
        class_replaceMethod(class,
                            swizzledSelector,
                            method_getImplementation(originalMethod),
                            method_getTypeEncoding(originalMethod));
    } else {
        method_exchangeImplementations(originalMethod, swizzledMethod);
    }
}

hook viewDidAppear

__gbh_tracer_swizzleMethod([self class], @selector(viewDidAppear:), @selector(__gbh_tracer_viewDidAppear:));

- (void)__gbh_tracer_viewDidAppear:(BOOL)animated{
    [self __gbh_tracer_viewDidAppear:animated];  //由于方法已经被交换,这里调用的实际上是viewDidAppear:方法
    
    //设置不允许发送数据的Controller
    NSArray *filter = @[@"UINavigationController",@"UITabBarController"];
    NSString *className = NSStringFromClass(self.class);
    if ([filter containsObject:className]) return ; //如果该Controller在不允许发送log的列表里,则不能继续往下走
    
    if ([self.title isKindOfClass:[NSString class]] && self.title.length > 0){ //有标题的才符合我的要求
        // 这里发送log
    }
    NSLog(@"%@:the view has been fully transitioned onto the screen",[self class]);
}

同等方式可以对其他方法进行 hook ,这样就能通过注入的方式,实现页面的统计了

详细代码:https://github.com/Supecomer/CIhookTest

结束语

正常开发中,如果项目架构完整,这样的方式是不需要的,相对来说,这样的方式达到的效果并不完美。技术没有好坏,只看怎么用;能力没有强弱,只看你熟不熟。

上一篇 下一篇

猜你喜欢

热点阅读