Method Swizzling
メソッド・スウィズリングは、セレクタの実際の実装を変更する技術です。このテクニックを使うと、クラスのディストリビューションテーブルでセレクタに対応する関数を修正することで、実行時にメソッドの実装を変更することができます。
実装
static inline void JJ_swizzleSelector(Class class, SEL originalSelector, SEL swizzledSelector) {
Method originalMethod = class_getInstanceMethod(class, originalSelector);
Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
if (class_addMethod(class, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod))) {
class_replaceMethod(class, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
} else {
method_exchangeImplementations(originalMethod, swizzledMethod);
}
}
@implementation UIApplication (Hook)
+ (void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Class class = [self class];
JJ_swizzleSelector(class, @selector(registerForRemoteNotifications), @selector(hook_registerForRemoteNotifications));
});
}
-(void)hook_registerForRemoteNotifications
{
//通知をオフにする
}
@end