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
5
votes
0 answers

Replace (swizzle) an entire class (with all instance and static methods) in Objective-C

I'm new to method swizzling and now I'd like to do a little more with that on iOS: Is there an easy/elegant way in Objective-C to redirect all calls of an existing class (ClassA) to another class (ClassB, provided it has the exact same methods). I…
Bedford
  • 1,136
  • 2
  • 12
  • 36
5
votes
1 answer

Swizzling methods, that implicitly return a retained object under ARC

For example, lets consider following code under ARC: #import #import @implementation NSDate (MyEvilHack) + (void)load { Method originalMethod = class_getInstanceMethod(self,…
Borys Verebskyi
  • 4,160
  • 6
  • 28
  • 42
5
votes
1 answer

Does method [load] need to call [super load]

I know that many methods need to call its super class method and some methods dont need, I'm looking about sth about method swizzling.It's initialized in the load method,and in the tutorial there is no [super load]. i'm wondering if it's wrong or…
Henson Fang
  • 1,177
  • 7
  • 30
5
votes
2 answers

Objective-c IOS arm64 method swizzling fail to call original method

I use standard method swizzling on ARMv7 IOS devices and it works perfect to me. But when I compile my code for arm64 - it fails to call original method from new method Main purpose of my swizzling - to use parameter from internal method of my…
4
votes
2 answers

how to swizzle didReceiveRemoteNotification of AppDelegate from framework

I'm developing a framework for iOS apps (a pod). I want to swizzle application(_:didReceiveRemoteNotification:fetchCompletionHandler:) with a method defined in my framework. this is my code: class MyClass { @objc func myCustomizedMethod(_…
4
votes
2 answers

How to swizzle Swift.print(items:separator:terminator)

I am looking for ways to swizzle Swift.print function. Overriding it is not an option, as it may get bypasses if you use Swift.print(:) The selector does not recognise the identifier: @objc class func printSwizzle() { guard let instance =…
Tal Zion
  • 6,308
  • 3
  • 50
  • 73
4
votes
1 answer

How to override a Swift class-property for testing

I've got a Swift class that I'm unit testing. The main purpose of the class is to make HTTP calls. I just finished mocking all networking requests using Mockingjay, but I want to make sure that going forward I don't forget to mock future requests.…
Dov
  • 15,530
  • 13
  • 76
  • 177
4
votes
5 answers

How to override NSDate description? ..Method Swizzling?

How can I call print(NSDate()) and instead of receiving the usual response, obtain the one I have in a function named getString() that is part of an extension of NSDate. Here's my extension: extension NSDate { //NSDate to String …
Hugo Alonso
  • 6,684
  • 2
  • 34
  • 65
4
votes
4 answers

Exchange implementations for C# methods

Is it possible to exchange implementations of methods in C#, like method swizzling in Objective-C? So I could replace an existing implementation (from an outside source, via a dll for example) at runtime with my own (or add another onto it). I have…
vrwim
  • 13,020
  • 13
  • 63
  • 118
4
votes
1 answer

Advanced method swizzling in Objective-C, iOS 9?

The question is simple. I need the same as usually - how to replace the original method with my one but @selector(presentViewController:animated:completion:) in iOS9? It works in iOS9 simulator only, not real device. Yes this code is called but then…
4
votes
1 answer

Method Swizzling for protocols

I want to be able to Swizzle protocol methods like textFieldDidBeginEditing: and tableView:didSelectRowAtIndexPath:. How can this be done?
YogevSitton
  • 10,068
  • 11
  • 62
  • 95
3
votes
2 answers

Objective-C: Methods called by a swizzled method should call the original implementation

I'm fixing bugs in someone else's closed-source app. In macOS, scrollbars can be set in System Preferences to display "always" (NSScrollerStyleLegacy), "when scrolling" (NSScrollerStyleOverlay), or "automatically based on mouse or trackpad"…
Wowfunhappy
  • 168
  • 7
3
votes
1 answer

Swift - Method swizzling for private framework's method and then invoking original implementation

I'm trying to swizzle a private framework's method to perform some custom logic and then would like to call the original implementation. Code: class SwizzlingHelper { private struct Constants { static let privateFrameworkClassName =…
Kunal Shah
  • 1,071
  • 9
  • 24
3
votes
0 answers

How to use method swizzling to change the system font used in iOS app?

I am creating an iOS app in which different fonts must be used for different languages. I am trying to do this by using systemFont everywhere in the app and at runtime, changing what systemFont refers to depending on the language. Following is the…
user11497206
3
votes
1 answer

ios swizzle better understanding

I have a UIViewController with this code: - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; NSLog(@"CLASIC"); } And then I have a framework with a UIViewController category that does swizzling in this manner: +…
Catalin
  • 1,821
  • 4
  • 26
  • 32