Question

I have url connection, which normally works fine

NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request
                                                              delegate:delegate];

But when I create a modal window, no request ever receives response:

[NSApp runModalForWindow:window];

If I comment this line out, thus creating a 'standard' window, everything works.

I tried implementing all methods from NSURLConnectionDelegate, not a single of them called.

I suspect this is something about 'run loops', but have little experience in this area. Does anybody have experience in this?

Thank you

Was it helpful?

Solution

If you're targeting 10.5+, you can tell the NSURLConnection to also run in NSModalPanelRunLoopMode (the mode your current thread's runloop would be in while presenting a modal view) via

-(void)scheduleInRunLoop:(NSRunLoop *)aRunLoop forMode:(NSString *)mode

where aRunLoop would probably be [NSRunLoop currentRunLoop] and the mode would be NSModalPanelRunLoopMode. More info in the NSURLConnection doc.

If you're supporting earlier OSs, you may have to get creative (i.e. with multithreading). Good discussion of this issue pre-10.5 here.

OTHER TIPS

I haven't bumped into the situation you're having, but I suggest spawning and starting a connection in background thread.

I also met the same problem that didn't get the delegate method called when using NSURLConnection in a Modal Window.

after some investigation, following code resolve it.

NSURLConnection* conn = [[NSURLConnection alloc] initWithRequest:requst delegate:self startImmediately:NO];
[conn scheduleRunLoop:[NSRunLoop currentLoop] forMode:NSModalPanelRunLoopMode];
[conn start];

However, when connectionDidFinishLoading called, [NSApp stopModal] doesn't work, need call [NSApp abortModal] instead.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top