runModalForWindow 限制 http 请求

发布于 2024-10-17 01:28:23 字数 453 浏览 2 评论 0原文

我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

淡淡の花香 2024-10-24 01:28:23

方式告诉 NSURLConnection 也以 NSModalPanelRunLoopMode 运行(当前线程的运行循环在呈现模态视图时所处的模式)

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

如果您的目标是 10.5+,您可以通过以下 aRunLoop 可能是 [NSRunLoop currentRunLoop]mode 可能是 NSModalPanelRunLoopMode。更多信息请参见 NSURLConnection 文档

如果您支持早期的操作系统,您可能必须发挥创意(即使用多线程)。 此处对此问题在 10.5 之前进行了很好的讨论。

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.

╰◇生如夏花灿烂 2024-10-24 01:28:23

我没有遇到您遇到的情况,但我建议在后台线程中生成并启动连接。

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

我爱人 2024-10-24 01:28:23

我也遇到了同样的问题,在模态窗口中使用 NSURLConnection 时没有调用委托方法。

经过一番调查后,以下代码解决了这个问题。

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

但是,当调用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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文