iOS 实例方法混淆
来源:互联网 发布:上海惠普软件电话 编辑:程序博客网 时间:2024/05/15 00:52
iOS 实例方法混淆
奇技yin巧 指过于奇巧而无益的技艺与制品.
转载请注明出处http://blog.csdn.net/uxyheaven/article/details/44265539
系列文章请看http://blog.csdn.net/uxyheaven/article/category/5800569
IMS指的是 Instance Method Swizzling, 实例方法混淆.
下段代码是一个Instance Method Swizzling和一个Method Swizzling的例子:
// Man.m- (void)run{ NSLog(@"%s, %@", __func__, _name);}- (void)jump{ NSLog(@"%s, %@", __func__, _name);}- (void)handsUp{ NSLog(@"%s, %@", __func__, _name);}- (void)handsDown{ NSLog(@"%s, %@", __func__, _name);}// ViewController.m- (void)viewDidLoad { ... Man *a = [Man manWithName:@"a"]; Man *b = [Man manWithName:@"b"]; [self swizzleInstanceMethodWithInstance:a originalSel:@selector(run) replacementSel:@selector(jump)]; [self swizzleInstanceMethodWithClass:[Man class] originalSel:@selector(handsUp) replacementSel:@selector(handsDown)]; [a run]; [b run]; [a handsUp]; [b handsUp];}// 输出的结果是2015-03-14 23:53:39.832 testRuntime[2196:629365] -[Man jump], a2015-03-14 23:53:39.833 testRuntime[2196:629365] -[Man run], b2015-03-14 23:53:39.833 testRuntime[2196:629365] -[Man handsDown], a2015-03-14 23:53:39.833 testRuntime[2196:629365] -[Man handsDown], b
为什么run方法是只有对象a被替换了,handsUp方法是都被替换了呢?
我们先来看下普通的Method Swizzling是如何实现的
- (void)swizzleInstanceMethodWithClass:(Class)clazz originalSel:(SEL)original replacementSel:(SEL)replacement{ Method a = class_getInstanceMethod(clazz, original); Method b = class_getInstanceMethod(clazz, replacement); // class_addMethod 为该类增加一个新方法 if (class_addMethod(clazz, original, method_getImplementation(b), method_getTypeEncoding(b))) { // 替换类方法的实现指针 class_replaceMethod(clazz, replacement, method_getImplementation(a), method_getTypeEncoding(a)); } else { // 交换2个方法的实现指针 method_exchangeImplementations(a, b); }}
Instance Method Swizzling是用了类似KVO的办法.
先动态添加一个类MySubclass继承自原来的类,然后修改对象a的isa为新类,再替换掉新类的方法.
- (void)swizzleInstanceMethodWithInstance:(id)object originalSel:(SEL)original replacementSel:(SEL)replacement{ Class newClass = objc_allocateClassPair([object class], "MySubclass", 0); objc_registerClassPair(newClass); Method a = class_getInstanceMethod(newClass, original); Method b = class_getInstanceMethod([object class], replacement); if (class_addMethod(newClass, original, method_getImplementation(b), method_getTypeEncoding(b))) { class_replaceMethod(newClass, replacement, method_getImplementation(a), method_getTypeEncoding(a)); } else { method_exchangeImplementations(a, b); } object_setClass(object, newClass);}
6 0
- iOS 实例方法混淆
- ios混淆方法名生成器
- Swift对象实例方法名混淆的解决
- iOS安全–浅谈关于iOS加固的几种方法,代码混淆,类名方法名混淆等
- iOS类方法、实例方法、构造方法
- iOS方法和实例变量
- Android 混淆文件实例
- iOS 类方法和实例方法
- iOS中类方法和实例方法
- iOS 类方法与实例方法总结
- iOS之代码混淆
- IOS代码混淆
- iOS代码混淆
- ios 代码混淆
- iOS代码混淆总结
- iOS代码混淆总结
- iOS学习笔记-iOS中的类方法和实例方法
- android代码混淆方法
- ASP.NET学习路线图
- Android ListView圆角实现
- Android下常见的内存泄露 经典
- win7安装黑苹果OS 10.9.1教程
- 致佳音: 推箱子游戏自动求解算法设计(四)
- iOS 实例方法混淆
- 黑马程序员——Java之编程基础(二)
- foj 2173 nostop
- Linux下消息队列和socket绝对速度比拼[转]
- 浅谈C/C++的浮点数在内存中的存储方式
- SRS微信号和QQ群
- LAR(最小角回归)
- gradle构建+proguard加密
- Java设计模式-21-单一职责原则