jin
jin

Reputation: 2155

How to show a splash screen during launch of a Cocoa app?

I would like to show the user a splash screen (a picture) while my Cocoa-based application launches. How would this be possible?


First thanks a lot. because my app running for a while time , so I want to show a splash before app running . Now if I show a window inside with a image , after that how to run the app? How to make sure that the app running after the splash showing ? How to do to get the sequence ?


First Thank you very much. And I show the window in applicationWillFinishLaunching method use orderFront,then hide it in applicationDidFinishLaunching: use orderOut,Now I found that the mainWindow not to show and the app terminate ,why ? How to do to resolute this question? Thanks!

Upvotes: 10

Views: 9227

Answers (4)

Taylor
Taylor

Reputation: 6410

Barry's answer above does not seem to work for document-based apps. Showing a splash window within applicationWillFinishLaunching: interferes with the startup sequence of the app such that the document window isn't created. I've uploaded an example project here. In applicationWillFinishLaunching:, comment out [_splashWindow orderFront:self ] and the document window will come up.

Upvotes: 0

Barry Wark
Barry Wark

Reputation: 107754

Although Peter's answer is ultimately correct (you should rewrite your app to launch faster), sometimes that's not a practical option. For example loading code later in the application may take too long (e.g. a data acquisition application), forcing it to be loaded at startup. If you decide that you want to show a splash screen, the easiest way is to show it in the application delegate's applicationWillFinishLaunching: method. Create a splash window in your applications MainMenu.nib and add an outlet to your app delegate referencing that window. You can then put the window onscreen in applicationWillFinishLaunching: and hide it in applicationDidFinishLaunching:. Note that the main thread's NSRunLoop is not iterating during this time, so if you want to update the splash screen (with status, a progress bar, or such), you'll need to manage those redraw events yourself.

Again, think very hard about whether the long startup is necessary. If it is, showing a splash screen with a progess indicator is the minimum that you owe your users.

Upvotes: 12

Mark
Mark

Reputation: 6128

Just put up a window with the image and close it when you are done with your launch initialization.

Upvotes: 0

Peter Hosey
Peter Hosey

Reputation: 96333

Why do you hate your users?

Seriously, don't do this. Don't make your users wait to use your app. Make your app launch quickly instead.

(And just in case you insist on an answer: Show a window with the image in it, then hide the window when you feel the user has waited long enough.)

Upvotes: 6

Related Questions