-2

I'm using a plugin,there is a method "A" in the plugin's pluginClass. if "A" is called ,I want to call "B" in myCalss.Such as:

-(void)A{
  [myClass B];
}

But I don't want to modify the code in pluginClass. Is there any way to add an observer to a method or similar? Thank u.

  • You could swizzle it, I suppose... – matt Mar 21 '16 at 03:41
  • I phrase my answer as "only acceptable option" because swizzling is always ugly :D – David Berry Mar 21 '16 at 03:43
  • Actually now I'm using swizzle,I'm trying to look for any other ways.Such as:```- (void)viewDidLoad { [super viewDidLoad]; NSMethodSignature *sayHi = [self methodSignatureForSelector:@selector(sayHi)]; [[NSNotificationCenter defaultCenter]addObserver:sayHi selector:@selector(sayHiRunning) name:@"" object:nil]; [self sayHi]; } -(void)sayHiRunning{ NSLog(@"bbbbbb"); } -(void)sayHi{ NSLog(@"aaaaaaa"); }```But it doesn't work. – Pikachuode Mar 21 '16 at 04:27
  • @DavidBerry "swizzling is always ugly" But legal, and fundamental to how certain Foundation features work. Key-value observing uses swizzling - and observing is what the OP want to do. – matt Mar 21 '16 at 14:43

2 Answers2

0

The only acceptable option I can think of is to subclass the class from the plugin, and then within your subclass over the target method and forward it:

@implementation MyPluginClass : PluginClass

-(void) A {
    [super A];
    [myClass B];
}

@end
David Berry
  • 40,941
  • 12
  • 84
  • 95
0

Seems really easy solution. Make your own method C that contains A function and add method B in C. Instead of calling A, then you call C.

// BEFORE

[pluginClass A]; // a call to method A that u use originally.

// AFTER

[self C];

-(void)C {
  [pluginClass A];
  [myClass B];
}
GeneCode
  • 7,545
  • 8
  • 50
  • 85
  • Thank u,but I must not to call "A" myself, it will be called by plugin all the time.And I just want to listen it. – Pikachuode Mar 21 '16 at 04:15