delovski
Joined: 14 Jun 2006 Posts: 3524 Location: Zagreb
|
Posted: Thu Mar 21, 2013 12:06 am Post subject: Command line tools |
|
|
so - NSURLConnection.. in the context of a command line utility
"Per the docs, the connection's delegate methods get called on the
same thread that the connection was started from. So, to keep that
thread running until the connection has time to do its stuff:"
Code: | int main(int argc, char *argv[])
{
// ...
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:del startImmediately:YES];
CFRunLoopRun(); // Run this run loop, run!
// ...
}
Then the delegate can stop the run loop when the connection says it's finished:
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
// This returns control to wherever you called
// CFRunLoopRun() from, so you can still clean up
// or do other interesting things.
CFRunLoopStop(CFRunLoopGetCurrent());
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"Error: %@", error);
CFRunLoopStop(CFRunLoopGetCurrent());
}
|
|
|