Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-C: Inject code into delegate method

What I want

I want to add some logging code into my app without having to update all of my view controllers.

What I've tried

I attempted to add a category to UIViewController and override one of the delegate methods. However, this produced several warnings/errors.

Is there a way for me to inject code into all of my app's ViewControllers?

like image 683
Jason McCreary Avatar asked Jul 03 '26 10:07

Jason McCreary


1 Answers

Look into method swizzling. It is a powerful tool and can save you a lot of time. Swizzle one of the methods you wish to have logs in, and then call the original method to have it do what it is originally supposed to.

like image 81
Léo Natan Avatar answered Jul 06 '26 02:07

Léo Natan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!