6

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
Dmitry
  • 14,306
  • 23
  • 105
  • 189

3 Answers3

21

The answer is very interesting:

[self swizzledMethod]; // will call originalMethod
Dmitry
  • 14,306
  • 23
  • 105
  • 189
3

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

Community
  • 1
  • 1
The Lazy Coder
  • 11,560
  • 4
  • 51
  • 69
0

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

Daij-Djan
  • 49,552
  • 17
  • 113
  • 135