8

I'm developing a phonegap plugin. So far so good. Now I would like to append 1 or 2 methods to the AppDelegate.m through the config.xml so it will be populated for the developer automatically. Is it possible?

Thanks.

Nithin Michael
  • 2,166
  • 11
  • 32
  • 56
Asaf
  • 2,158
  • 2
  • 25
  • 40

2 Answers2

7

take a look into the Push Plugin, they use an objective-c category for the appDelegate

https://github.com/phonegap-build/PushPlugin

If you just want to be notified when the app becomes active you don't need to change anything on the AppDelegate, just put this on your plugin:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onAppDidBecomeActive:) name:UIApplicationDidBecomeActiveNotification object:nil]; 

- (void)onAppDidBecomeActive:(NSNotification*)notification
{

     NSLog(@"%@",@"applicationDidBecomeActive");

}
jcesarmobile
  • 51,328
  • 11
  • 132
  • 176
  • Thanks, but I'm not sure how it works. In the "regular" AppDelegate, will I be notify on app become active and etc.? – Asaf Mar 05 '14 at 08:22
  • If you just want to be notified when the app becomes active you don't need to change anything on the AppDelegate, just put this on your plugin: [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onAppDidBecomeActive:) name:UIApplicationDidBecomeActiveNotification object:nil]; – jcesarmobile Mar 05 '14 at 08:58
0

If you do need to override some other method, like if iOS doesn't issue a notification for some app delegate method, you can do it with method swizzling. jcesarmobile mentioned this repo in his answer, but a file with an example is:

https://github.com/phonegap-build/PushPlugin/blob/master/src/ios/AppDelegate%2Bnotification.m

Crag
  • 1,723
  • 17
  • 35