runModalForWindow 限制 http 请求
我有 url 连接,通常工作正常
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request
delegate:delegate];
但是当我创建模式窗口时,没有请求收到响应:
[NSApp runModalForWindow:window];
如果我注释掉这一行,从而创建一个“标准”窗口,一切都会正常。
我尝试实现 NSURLConnectionDelegate 中的所有方法,但没有调用其中的任何一个。
我怀疑这与“运行循环”有关,但在这方面经验很少。有人有这方面的经验吗?
谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
方式告诉
NSURLConnection
也以NSModalPanelRunLoopMode
运行(当前线程的运行循环在呈现模态视图时所处的模式)如果您的目标是 10.5+,您可以通过以下
aRunLoop
可能是[NSRunLoop currentRunLoop]
,mode
可能是NSModalPanelRunLoopMode
。更多信息请参见NSURLConnection
文档。如果您支持早期的操作系统,您可能必须发挥创意(即使用多线程)。 此处对此问题在 10.5 之前进行了很好的讨论。
If you're targeting 10.5+, you can tell the
NSURLConnection
to also run inNSModalPanelRunLoopMode
(the mode your current thread's runloop would be in while presenting a modal view) viawhere
aRunLoop
would probably be[NSRunLoop currentRunLoop]
and themode
would beNSModalPanelRunLoopMode
. More info in theNSURLConnection
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.
我没有遇到您遇到的情况,但我建议在后台线程中生成并启动连接。
I haven't bumped into the situation you're having, but I suggest spawning and starting a connection in background thread.
我也遇到了同样的问题,在模态窗口中使用 NSURLConnection 时没有调用委托方法。
经过一番调查后,以下代码解决了这个问题。
但是,当调用connectionDidFinishLoading时,[NSApp stopModal]不起作用,需要调用[NSApp abortModal]。
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.
However, when connectionDidFinishLoading called, [NSApp stopModal] doesn't work, need call [NSApp abortModal] instead.