函数混淆之Method Swizzling

Method Swizzling是改变一个selector的实际实现的技术。通过这一技术,我们可以在运行时通过修改类的分发表中selector对应的函数,来修改方法的实现。通过这种方法可以实现函数混淆。

 static dispatch_once_t onceToken;

    dispatch_once(&onceToken, ^{

        Method sampleMethod = class_getInstanceMethod([self class], @selector(sample));

        Method difficultyMethod = class_getInstanceMethod([self class], @selector(difficulty));

        method_exchangeImplementations(sampleMethod, difficultyMethod);

    });


这样可以动态的实现函数实现的替换。

在使用Swizzling中需要注意几点:

1.由于method swizzling会影响到类的全局状态,因此要尽量避免在并发处理中出现竞争的情况。

扫描二维码关注公众号,回复: 10618962 查看本文章

2.给自定义的分类方法加前缀,从而使其与所依赖的代码库不会存在命名冲突。

发布了16 篇原创文章 · 获赞 23 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/DARKSang/article/details/45969391