Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method Swizzle on iPhone device

Tags:

I tried both JRSwizzle, and MethodSwizzle. They compile fine on the simulator but throw a bunch of errors when I try to compile for Device (3.x)

Has anyone had any luck swizzling on the iphone? Whats the trick?

TIA

like image 680
dizy Avatar asked Oct 28 '09 14:10

dizy


People also ask

What is method swizzling and when do you use it?

Method swizzling is the process of replacing the implementation of a function at runtime. Swift, as a static, strongly typed language, did not previously have any built-in mechanism that would allow to dynamically change the implementation of a function.

How do I disable method swizzling?

Method swizzling in Firebase Cloud Messaging Developers who prefer not to use swizzling can disable it by adding the flag FirebaseAppDelegateProxyEnabled in the app's Info. plist file and setting it to NO (boolean value).

Have you heard about method swizzling what is that can it be done in Swift?

Swizzling (other languages call this “monkey patching”) is the process of replacing a certain functionality or adding custom code before the original code is called. For example, you could swizzle UIViewController. viewDidAppear to be informed whenever a view controller is displayed.


1 Answers

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));     else     method_exchangeImplementations(origMethod, newMethod); } 

I have not tested this, simply because I regard method swizzling as an extremely dangerous process and haven't had the need to use it yet.

like image 56
Brad Larson Avatar answered Sep 20 '22 12:09

Brad Larson