Dmitry
Dmitry

Reputation: 14632

How to call the original method from swizzled one?

How to call the original method from swizzled one?

The original method is replaced by the code:

[[UIWindow class] jr_swizzleMethod:@selector(originalMethod) withMethod:@selector(swizzledMethod) error:nil];

The following code on swizzledMethod makes recursion!

[self originalMethod];

How to solve this problem?

I use the following library for swizzling:

// JRSwizzle.h semver:1.0
//   Copyright (c) 2007-2011 Jonathan 'Wolf' Rentzsch: http://rentzsch.com
//   Some rights reserved: http://opensource.org/licenses/MIT
//   https://github.com/rentzsch/jrswizzle

#import <Foundation/Foundation.h>

@interface NSObject (JRSwizzle)

+ (BOOL)jr_swizzleMethod:(SEL)origSel_ withMethod:(SEL)altSel_ error:(NSError**)error_;
+ (BOOL)jr_swizzleClassMethod:(SEL)origSel_ withClassMethod:(SEL)altSel_ error:(NSError**)error_;

@end

Upvotes: 6

Views: 3372

Answers (3)

The Lazy Coder
The Lazy Coder

Reputation: 11838

I had gone through creating method swizzling for iOS 5

and I put up an explanation of it here.

Method Swizzling in iOS 5?

essentially every call to the original method is going to yours. And therefore every call to your method should be directed back to the original. (if the swizzle was set up correctly)

Hope that helps

Upvotes: 3

Daij-Djan
Daij-Djan

Reputation: 50109

call your own method... you swizzeled the pointers but not the names. e.g.

originalFunc=$123 myFunc=$321

swizzle =>

originalFunc=$321 myFunc=$123

so to call the original $123 we now call myFunc

Upvotes: 0

Dmitry
Dmitry

Reputation: 14632

The answer is very interesting:

[self swizzledMethod]; // will call originalMethod

Upvotes: 21

Related Questions