blog

Objective-Cフック・スキーム / メソッド・スウィズリング

メソッド・スウィズリングは、セレクタの実際の実装を変更するためのテクニックです。この技法では、クラスのディストリビューションのセレクタに対応する関数を修正することで、実行時にメソッドの実装を変更するこ...

Jun 20, 2020 · 1 min. read
シェア

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
Read next

JavaScriptにおけるクッキーの単純なカプセル化

cookieはカプセル化する必要があります。ネイティブのcookieインターフェースは十分にフレンドリーではないので、練習のために単純なバージョンのcookieをカプセル化しました。

Jun 19, 2020 · 3 min read