As others have answered, it's possible to implement what you want to do in a way you suggested, i.e., by running a while
loop inside main
.
However, that is not the best way to write a Cocoa app which reloads a URL once in a few seconds. In a different environment, there's a different standard way to do things. So, you sometimes need to un-learn what you got used to. You might have thought: I want to do X
. In language/environment A
, I would have coded like P
to do X
. Now I'd like to use language/environment B
. How should I implement P
? That's not the way to get used to a new environment. Just ask, How should I do X
in the environment B
?
The most Cocoa-esque way would be this:
- Open XCode, create a new project, choose a Cocoa GUI app from the template.
In the application delegate, implement applicationDidFinishLaunching:
. We are going to set up an NSTimer
.
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
NSTimer*timer=[NSTimer scheduledTimerWithTimeInterval:5
target:self
selector:@selector(timerFired:)
userInfo:nil
repeats:YES];
}
This creates a timer which fires once in five seconds, and at each time it fires it calls the method timerFired:
of the app delegate itself, specified by self
. For more on NSTimer
, read this apple document.
Implement timerFired:
.
- (void)timerFired:(NSTimer*)theTimer{
// do whatever you want. you can use plain C to invoke curl,
// or if you want you can use Cocoa methods to access a URL.
}
There's no fourth step!
The main
function is provided by the template. It calls NSApplicationMain
, which set up the Cocoa system. Eventually, it calls applicationDidFinishLaunching:
of your delegate for you. You respond to that message. Then you set up a timer. The timer calls the method you specified for you. Then you respond to that message, again. That's basically how Cocoa works. The Cocoa system asks you to do something, so you do something. Your control over the flow of the program becomes rather passive, compared to what you would have programmed in Applescript.