Re: applicationWillFinishLaunching
Re: applicationWillFinishLaunching
- Subject: Re: applicationWillFinishLaunching
- From: Peter <email@hidden>
- Date: Fri, 30 Sep 2011 09:37:55 +0200
I am not sure I get your first sentence right, but rather than relying on the automatic delegate notification mechanism, you might want to actively subscribe to the notification. You can do so from any object, not just a delegate.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(finishInitialization)
name:NSApplicationWillFinishLaunchingNotification
object:NSApp];
If your app needs to do something at this stage which requires the existence and activity of a run loop you could plug in a run loop observer once for the first cycle of the run loop.
In something like your appController you implement a method and a function to set things up:
void MyRunLoopObserver(CFRunLoopObserverRef observer,
CFRunLoopActivity activity,
void* info)
{
//replace MyAppController here by the name of the class of self
//refToSelf gives you access to everything (methods and ivars etc.) of self
//doSomething here is the method you want to call when the run loop starts
[(MyAppController *)refToSelf doSomething];
}
- (void) installRunLoopObserver
{
CFRunLoopObserverRef myObserver = NULL;
myObserver = CFRunLoopObserverCreate(NULL,
kCFRunLoopEntry, /* plug in at the beginning of the run loop */
NO, /* do not repeat */
0,
&MyRunLoopObserver,
0);
if (myObserver)
{
// Now add it to the run loop
CFRunLoopAddObserver(CFRunLoopGetCurrent(),
myObserver,
kCFRunLoopCommonModes);
}
CFRelease(myObserver);
}
Then, in the same class, from somewhere early in the app launch process, e.g. awakeFromNib, you set a global void * refToSelf (declared outside @implementation)
refToSelf = self; //self, BTW, could be replaced by any object implementing the doSomething method
and call
[self installRunLoopObserver]; //whereas self here, of course, is the object calling installRunLoopObserver on itself
When the app enters the first run loop, doSomething is executed.
Note that the example in the documentation (of which the above is derived) does not work as advertised. (I had consistent crashes when working with *info as a container for self. Therefore I use the global refToSelf). This one works - although I can not give you any guarantee that it will work out for your special condition "plug into the first run loop cycle of the app", since I haven't tried this special case. But since everything should be set up properly at this point, it should work out.
Hope this helps.
Am 30.09.2011 um 01:48 schrieb koko:
> I have implemented applicationWillFinishLaunching in my app delegate but strangely (to me) it is not called but applicationDidFinishLaunching is called so I know my delegate is properly connected.
>
> Are there some conditions that must be met for applicationWillFinishLaunching to be called?
>
> What I want to do is ask for a serial number before the main window is displayed as the serial number will determine the app configuration and hence what appears in the main window. So, I thought I would ask for the serial number in applicationWillFinishLaunching thinking the main window will yet be visible. I would the configure the app.
>
> If there is a better way to accomplish this sequence of events I am all ears.
>
> -koko
>
> _______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
>
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden