RunModalForWindow handles HTTP requests

I have a url connection that works fine fine

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

But when I create a modal window, the request does not receive a response:

 [NSApp runModalForWindow:window]; 

If I comment on this line by creating a β€œstandard” window, everything will work.

I tried to implement all the methods from NSURLConnectionDelegate, and not one of them.

I suspect this is something like "run loops", but I have little experience in this area. Does anyone have any experience with this?

thanks

+4
source share
3 answers

If you target 10.5+, you can tell NSURLConnection also run in NSModalPanelRunLoopMode (the mode in which the current streaming runloop will be displayed when presenting a modal view) through

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

where aRunLoop will probably be [NSRunLoop currentRunLoop] and mode will be NSModalPanelRunLoopMode . Further information in NSURLConnection doc .

If you support earlier OSs, you may need to create ads (i.e. using multithreading). A good discussion of this issue until 10.5 is here .

+8
source

I did not encounter the situation you are experiencing, but I suggest spawning and starting the connection in the background thread.

+1
source

I also encountered the same problem that did not invoke the delegate method when using NSURLConnection in a modal window.

after some investigation, the following code will resolve it.

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

However, when the connectionDidFinishLoading called, [NSApp stopModal] does not work, you must call [NSApp abortModal].

+1
source

Source: https://habr.com/ru/post/1339602/


All Articles