ios在系统代码中注入自己的代码
2013-08-17 01:07
323 查看
转自:http://blog.csdn.net/shaobojohn/article/details/7944011
今天pm提了一个新的需求,需要监听整个程序的事件,在网上查询了下实现的方式。
在stackoverflow中发现了一种实现方式,创建UIApplication的一个子类,在这个子类中覆盖UIApplication中的sendEvent方法,在其中实现自己的功能之后,然后调用UIApplication的sendEvent方法来实现事件分发,代码如下:
子类的名称为MyApplication.
在 main.m 中使用:
[cpp]
view plaincopy
return UIApplicationMain(argc, argv, @"MyApplication", @"MyApplicationDelegate");
然后覆盖[MyApplication sendEvent:]
[cpp]
view plaincopy
- (void)sendEvent:(UIEvent*)event {
//handle the event (you will probably just reset a timer)
[super sendEvent:event];
}
但是这个问题需要继承UIApplication,感觉不太符合我的要求。所以我就考虑到Category来自己实现sendEvent,然后再其中实现自己功能后,来实现系统的分发,但是在网络上查找了sendEvent的源代码,没有任何发现,突然想到之前使用过的方法替换的方法,就想到了下列方法,具体代码如下:
发现这个方法也可以对其他的系统代码进行代码的注入
[cpp]
view plaincopy
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方法
[cpp]
view plaincopy
- (void)mySendEvent:(UIEvent*)event {
//handle the event (you will probably just reset a timer)
[self mySendEvent:event];
}
看到上面方法,直接调用肯定是死循环的,所以不能直接调用的,这个方法是提供给上面的Swizzle进行调用的,这样就将这两个方法进行替换,就是调用下面方法
[cpp]
view plaincopy
Swizzle([UIApplication class], @selector(sendEvent:), @selector(mySendEvent:))
实际就是将这两个方法信息的名称和它们对应的地址进行互换,
1)所以在在系统调用sendEvent的时候实际调用的是mySendEvent
2)mySendEvent方法中调用的mySendEvent实际调用的就是系统的sendEvent
这样就可以实现在sendEvent方法中注入自己需要的代码了。其他的系统方法也可以同样实现注入自己需要的代码。
今天pm提了一个新的需求,需要监听整个程序的事件,在网上查询了下实现的方式。
在stackoverflow中发现了一种实现方式,创建UIApplication的一个子类,在这个子类中覆盖UIApplication中的sendEvent方法,在其中实现自己的功能之后,然后调用UIApplication的sendEvent方法来实现事件分发,代码如下:
子类的名称为MyApplication.
在 main.m 中使用:
[cpp]
view plaincopy
return UIApplicationMain(argc, argv, @"MyApplication", @"MyApplicationDelegate");
然后覆盖[MyApplication sendEvent:]
[cpp]
view plaincopy
- (void)sendEvent:(UIEvent*)event {
//handle the event (you will probably just reset a timer)
[super sendEvent:event];
}
但是这个问题需要继承UIApplication,感觉不太符合我的要求。所以我就考虑到Category来自己实现sendEvent,然后再其中实现自己功能后,来实现系统的分发,但是在网络上查找了sendEvent的源代码,没有任何发现,突然想到之前使用过的方法替换的方法,就想到了下列方法,具体代码如下:
发现这个方法也可以对其他的系统代码进行代码的注入
[cpp]
view plaincopy
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方法
[cpp]
view plaincopy
- (void)mySendEvent:(UIEvent*)event {
//handle the event (you will probably just reset a timer)
[self mySendEvent:event];
}
看到上面方法,直接调用肯定是死循环的,所以不能直接调用的,这个方法是提供给上面的Swizzle进行调用的,这样就将这两个方法进行替换,就是调用下面方法
[cpp]
view plaincopy
Swizzle([UIApplication class], @selector(sendEvent:), @selector(mySendEvent:))
实际就是将这两个方法信息的名称和它们对应的地址进行互换,
1)所以在在系统调用sendEvent的时候实际调用的是mySendEvent
2)mySendEvent方法中调用的mySendEvent实际调用的就是系统的sendEvent
这样就可以实现在sendEvent方法中注入自己需要的代码了。其他的系统方法也可以同样实现注入自己需要的代码。
相关文章推荐
- ios在系统代码中注入自己的代码
- ios在系统代码中注入自己的代码
- phonegap(cordova) 自己定义插件代码篇(五)----android ,iOS 集成微信登陆
- [编写高质量iOS代码的52个有效方法](十一)系统框架
- iOS 自定义view 如何不使用self.view也就是系统自带的 view,使用自己定制的view
- iOS:使用Github托管自己本地的项目代码方式一:(Xcode方式:开发工具Xcode配置Git,由Xcode-->Source Control-->Commit)
- iOS_根据系统版本编译不同的代码
- iOS利用代码添加事件到系统日历中
- 解决iOS8.0以上系统下无法通过代码创建被删除过的同名相册 ( iOS 8 Photos framework: Create a albums with iOS8 )
- IOS APP 国际化(实现不跟随系统语言,不用重启应用,代码切换stroyboard ,xib ,图片,其他资源)
- 由成幻OnlineJudge学习如何做自己的Acm-Icpc在线评判系统-5.在线编译与测试系统代码粗解
- xcode反汇编调试iOS模拟器程序(八)反汇编自己的代码来掌握规则
- 依赖注入——让iOS代码更简洁
- IOS获取系统相册中照片的示例代码
- PHP以及js识别android/ios系统代码参考
- 维护一套同时兼容 iOS 6 和 iOS 7,并且能够自动适应两个系统的 UI 风格的代码
- IOS上传照片(自己的代码整理了一下)
- 史上最简单教程:向第三方jar包注入自己的代码
- ios开发之打开系统及自己的应用程序
- iOS系统设置中显示自己应用