Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
General
GeneralPortable MacsHardwareNetworking
Applications
Mac ApplicationsEudoraFirefox / MozillaInternet ExplorerOutlook ExpressMS OfficeEntourageExcelPowerPointWordVirtual PCMedia PlayerOther MS Products
Programming
Mac ProgrammingCodeWarriorPerl
Country Specific
Australian Mac GroupUK Mac Group

Mac Forum / Programming / Mac Programming / June 2006



Tip: Looking for answers? Try searching our database.

Startup code and event handling in windowless Cocoa app

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Tom - 20 Jun 2006 19:54 GMT
Hi,

This is such a basic question I'm kind of embarrassed to ask it, but
I'm new to Cocoa, and haven't managed to find the answer in any
documentation.  I'm writing a Cocoa app that runs a local web server.
It has no main window, but may eventually have a preferences window.
What I don't know is where to put the code to start up the web server -
I assume there's an application start event I can connect to my code,
but what? Or does it just go before the call to NSApplicationMain() in
main.m?  (I'm running it in a new NSThread, if that makes any
difference.)

I also want to respond to Dock icon clicks by opening a URL in the
default web browser. Any help with this would also be much appreciated!

thanks!
Tom
Tom Harrington - 20 Jun 2006 20:03 GMT
> Hi,
>
[quoted text clipped - 7 lines]
> main.m?  (I'm running it in a new NSThread, if that makes any
> difference.)

If you're calling NSApplicationMain(), then
-applicationDidFinishLaunching is probably a good place to put your
startup code.

Signature

Tom "Tom" Harrington
Macaroni, Automated System Maintenance for Mac OS X.
Version 2.0:  Delocalize, Repair Permissions, lots more.
See http://www.atomicbird.com/

Tom - 20 Jun 2006 20:21 GMT
> ...
> > I assume there's an application start event I can connect to my code,
[quoted text clipped - 5 lines]
> -applicationDidFinishLaunching is probably a good place to put your
> startup code.

That's in a delegate connected to the main app object, right?  (I've
just been googling some more.)

I should probably read the O'Reilly book or something..

cheers!
Tom
Reinder Verlinde - 20 Jun 2006 20:54 GMT
> Hi,
>
[quoted text clipped - 7 lines]
> main.m?  (I'm running it in a new NSThread, if that makes any
> difference.)

In article <tph-BE40DA.13032520062006@localhost>, Tom Harrington
answered that question.

I have a tiny bit to add: you will probably want to split the 'local web
server' part out of the 'may eventually have a preferences window' part.

Depending on what your app is supposed to do, solutions may be:
- two applications, one of which is faceless
- a control panel for the preferences and an application

> I also want to respond to Dock icon clicks by opening a URL in the
> default web browser. Any help with this would also be much appreciated!

I believe detecting the click is done by trapping a reopen event in your
application delegate:

   (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication
                               hasVisibleWindows:(BOOL)flag

There may not be a reliable way to discriminate this from a double-click
of an already-opened application.

Launching a URL is described in

<http://developer.apple.com/documentation/Carbon/Conceptual/LaunchService
sConcepts/LSCTasks/chapter_3_section_2.html>

[the easier to find <http://developer.apple.com/qa/nw/nw60.html> is a
red herring. That example is 'a bit' old (literally from last century)]

Reinder
Tom - 21 Jun 2006 09:07 GMT
>... I believe detecting the click is done by trapping a reopen event in your
> application delegate:
[quoted text clipped - 6 lines]
>
> Launching a URL is described in ...

Cool! Everything's working now!  Thanks, Reinder.

Tom
Simon Slavin - 22 Jun 2006 22:14 GMT

> I have a tiny bit to add: you will probably want to split the 'local web
> server' part out of the 'may eventually have a preferences window' part.
>
> Depending on what your app is supposed to do, solutions may be:
> - two applications, one of which is faceless
> - a control panel for the preferences and an application

Yes yes yes.  Definitely don't try to add a window to a server
application.  Your users might want to run the application when nobody is
logged-into the computer.

A control panel is one way to do it.  A simple application is another.
For bonus points, instead of writing either of those, write an application
which can actually be run on a different computer -- e.g. the one on administrator's desk -- instead of the computer that the server is running on.  Since you write that you're actually writing a local web server, you might find it easy to do this by adding a web interface.

The client/server design, whereby nobody is expected to type on the computer that runs the server, is an excellent piece of design which leads to added convenience for administrators and greater security for the server computer (where security is incredibly important).

Simon.
Signature

http://www.hearsay.demon.co.uk

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.