Method Swizzle on iPhone device

The CocoaDev wiki has an extensive discussion on method swizzling here. Mike Ash has a relatively simple implementation at the bottom of that page: #import <objc/runtime.h> #import <objc/message.h> //…. 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)); … Read more

How to swizzle a class method on iOS?

Turns out, I wasn’t far away. This implementation works for me: void SwizzleClassMethod(Class c, SEL orig, SEL new) { Method origMethod = class_getClassMethod(c, orig); Method newMethod = class_getClassMethod(c, new); c = object_getClass((id)c); 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); }

How to implement method swizzling swift 3.0?

First of all dispatch_once_t is unavailable in Swift 3.0. You can choose from two alternatives: Global variable Static property of struct, enum or class For more details, see that Whither dispatch_once in Swift 3 For different purposes you must use different implementation of swizzling Swizzling CocoaTouch class, for example UIViewController; Swizzling custom Swift class; Swizzling … Read more