ios在系统代码中注入自己的代码
来源:互联网 发布:基础网络知识书籍 编辑:程序博客网 时间:2024/06/05 04:09
转自:http://blog.csdn.net/shaobojohn/article/details/7944011
今天pm提了一个新的需求,需要监听整个程序的事件,在网上查询了下实现的方式。
在stackoverflow中发现了一种实现方式,创建UIApplication的一个子类,在这个子类中覆盖UIApplication中的sendEvent方法,在其中实现自己的功能之后,然后调用UIApplication的sendEvent方法来实现事件分发,代码如下:
子类的名称为MyApplication.在 main.m 中使用:
- return UIApplicationMain(argc, argv, @"MyApplication", @"MyApplicationDelegate");
- - (void)sendEvent:(UIEvent*)event {
- //handle the event (you will probably just reset a timer)
- [super sendEvent:event];
- }
发现这个方法也可以对其他的系统代码进行代码的注入
- void Swizzle(Class c, SEL orig, SEL new) {
- Method origMethod = class_getInstanceMethod(c, orig);
- Method newMethod = class_getInstanceMethod(c, new);
- if(class_addMethod(c, orig, method_getImplementation(newMethod), method_getTypeEncoding(newMethod)))
- class_replaceMethod(c, new, method_getImplementation(origMethod), method_getTypeEncoding(origMethod));
- else
- method_exchangeImplementations(origMethod, newMethod);
- }
然后定义自己的sendEvent方法
- - (void)mySendEvent:(UIEvent*)event {
- //handle the event (you will probably just reset a timer)
- [self mySendEvent:event];
- }
看到上面方法,直接调用肯定是死循环的,所以不能直接调用的,这个方法是提供给上面的Swizzle进行调用的,这样就将这两个方法进行替换,就是调用下面方法
- Swizzle([UIApplication class], @selector(sendEvent:), @selector(mySendEvent:))
实际就是将这两个方法信息的名称和它们对应的地址进行互换,
1)所以在在系统调用sendEvent的时候实际调用的是mySendEvent
2)mySendEvent方法中调用的mySendEvent实际调用的就是系统的sendEvent
这样就可以实现在sendEvent方法中注入自己需要的代码了。其他的系统方法也可以同样实现注入自己需要的代码。
- ios在系统代码中注入自己的代码
- ios在系统代码中注入自己的代码
- ios在系统代码中注入自己的代码
- 在ipa中注入动态库dylib并运行自己的代码
- 破解APK并注入自己的代码
- 在另一个进程中注入代码的方式20171013
- 远程进程注入 - 把自己的代码注入explorer.exe
- Android在WebView中注入Js代码
- iOS模拟器代码注入
- Dll注入系统进程的部分代码
- 在自己的java代码中编译并运行程序
- 如何在网页中运行自己的代码
- 我自己的代码---在存储过程中创建视图
- 在项目中添加自己的代码注释
- c# - 如何在自己的代码中使用Performance Counters
- 在Makefile中给自己的代码传递宏定义
- 在工程中查找自己修改的所有代码
- 利用CodeSmith在自己的工程中动态生成代码
- Android Service与Activity之间通信的几种方式
- RoboGuice使用心得,什么是RoboGuice?
- Java中的DatagramPacket与DatagramSocket的初步
- Hadoop的MapReduce框架中map和reduce的各自任务(能力工场--整理)
- UVA 10341 - Solve It(二分)
- ios在系统代码中注入自己的代码
- Ubuntu虚拟机 android 4.3 源码下载时无法访问googlesource的解决方法
- spring mvc测试之配置
- iOS5编程 -- main函数研究
- D3D中D3DFVF_XYZ和D3DFVF_XYZRHW的区别
- typedef精讲
- iOS应用程序状态切换相关
- Code Fragment-对于if语句中的boolean值分开判断的处理。
- ArrayList的使用