Questions tagged [method-swizzling]

Method swizzling is the process of changing the implementation of an existing method.

Method swizzling is the process of changing the implementation of an existing method, swapping its implementation with another's that have the same signature.

The technique is possible in languages where the method implementation is allowed to be changed at runtime. One example is Objective-C, in which is used by Apple’s Foundation Framework to implement Key-Value Observing.

108 questions
3
votes
0 answers

Method swizzling in an ObjC SDK

I am working on writing a new HTTP Library sort of SDK. For implementing cross cutting concerns we are planning to write middleware support similar to c# i.e. mechanism to handle retries, redirection, logging etc. Will it be a good idea to use…
Vikas Dadheech
  • 1,672
  • 12
  • 23
3
votes
1 answer

How to create categories dynamically in objective c?

I'm working on an objective-c library project ('MyLib'). Let's say this library is then included in 'TestApp'. Now, 'TestApp' also includes another library called 'Xlib'. This Xlib has a class C1 which has a method m1. //C1.h is part of…
3
votes
1 answer

Method swizzle not working on iOS 10.1

I want to swizzle the method @property string in UIPasteboard: + (void) load { .... [UIPasteboard jr_swizzleMethod:@selector(string) withMethod:@selector(stringSwizzle) error:nil]; [UIPasteboard jr_swizzleMethod:@selector(setString:)…
waitianlou
  • 563
  • 3
  • 15
3
votes
1 answer

Is it possible to disable method swizzling in a swift project?

I have a very large project with 3rd party frameworks and would like to disable method swizzling to make sure the 3rd party frameworks do not mess the default expected behaviour. Is it possible? Is there some flag in the project settings?
Yasmin Tiomkin
  • 282
  • 1
  • 5
  • 14
3
votes
0 answers

Timing issues with swift class with dynamic variable, which inherits from objective c, where the get/set methods are replaced

I have a very specific issue where, if I have a swift class which inherits from an objective-c class, and it has a dynamic property. Now, on the +initialize, I am injecting getter and setters into the swift(:NSObject) class, and these work no…
Adrian_H
  • 1,548
  • 1
  • 14
  • 27
3
votes
1 answer

Swift - extending a class only when it conforms to a protocol to use method swizzling

I have a protocol called NakedNavigationBar. I also have an extension that extends all UIViewControllers that conform to NakedNavigationBar. The problem is that in the extension I want to add default behaviour so that when the UIViewController…
Adam Carter
  • 4,741
  • 5
  • 42
  • 103
3
votes
0 answers

Swap method implementations (same signature) using Reflection?

I have to augment some methods in a closed source library. I was searching for some really simple way to swap implementations of two static methods (with entirely the same signature). I found MethodRental.SwapMethodBody, which I really like, but…
Geri Borbás
  • 15,810
  • 18
  • 109
  • 172
3
votes
0 answers

Swizzle something that was retained

Hi all I don't understand retain so well. There is this code here: mAppIcon = [[NSImage imageNamed:@"NSApplicationIcon"] retain]; I am trying to swizzle it so whenever they ask for image with name NSApplicationIcon I give it my custom icon. I still…
Noitidart
  • 35,443
  • 37
  • 154
  • 323
3
votes
1 answer

Method Swizzling in Objective-C

I read an article about "Method Swizzling in Objective-C". In this article the meaning of "Method Swizzing" is to exchange the implementations of two methods. The sample is as below shows: - (void) logged_viewDidAppear:(BOOL)animated { [self…
Jay
  • 113
  • 1
  • 11
3
votes
1 answer

Unit testing a singleton: mock and swizzle

I am currently trying to test some code that uses AVAudioSession and my attempts to mock it since it's a singleton have proved difficult thus far, and i did a bit of research and came across the idea to swizzle the way it get's it's instance to then…
Genhain
  • 1,937
  • 1
  • 19
  • 38
3
votes
1 answer

Why does ARC cause EXC_BAD_ACCESS when swizzling functions using class_replaceMethod from objc runtime library?

I need to replace some methods’ implementations of specific Objective-C classes. A set of functions from objc/runtime library is capable of doing that. To simplify the issue I just write a simplest sample code as following: #import…
2
votes
1 answer

Tracking All Events of ViewController and UIControls for Custom Analytics Framework iOS

I am working on an analytics SDK which will track all the user events which view is appeared or disappeared, Which button is clicked, Which UISwitch is turned ON or OFF, UITableView is scrolled or cell is tapped etc. I am using method swizzling for…
Abhishek
  • 1,654
  • 2
  • 18
  • 31
2
votes
1 answer

Swizzling UIImage init not working iOS Swift

I am trying to swizzle UIImage.init(named:) but the init is not being called extension UIImage { @objc public convenience init?(swizzledName: String) { self.init(named: swizzledName) /// Do something print("this is…
Tal Zion
  • 6,308
  • 3
  • 50
  • 73
2
votes
1 answer

Swizzling UIImage.init(named:) returns nil when getting the instance method

I am trying to swizzle UIImage init functions but when trying to get the instance function they return nil. Any idea? let instance = class_getInstanceMethod(self, #selector(UIImage.init(named)) let instanceWithBundle = class_getInstanceMethod(self,…
Tal Zion
  • 6,308
  • 3
  • 50
  • 73
2
votes
1 answer

Does Objective-C Method Swizzling affect code in other process?

Does Objective-C Method Swizzling affect code in other process? For example, I do the method swizzling on the -[NSArray count] in my app. Will the code in other processes be affected by that method swizzling? AFAIU method resolution should be within…