What's wrong on following URLConnection?

隐身守侯 提交于 2019-12-01 10:28:35
Mike Abdullah

Firstly you're making starting the connection too complicated. Change to:

connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]

Remove [connection start]. Now:

  • Is your app definitely running the run loop normally? NSURLConnection requires this to work.
  • Are you able to perform a synchronous load of the URL request?
  • In the debugger, can you see that url is what you expect it to be? What is it?
  • Is it possible that you're deallocating WSHelper before any delegate messages are received? NSURLConnection is asynchoronous after all.

One does not need to do anything special to use NSURLConnection, it's a straightforward part of the Foundation framework. No special compiler settings required. No initialization before use. Nothing. Please don't start blindly trying stuff like bringing in CoreServices.Framework.

As sending the request synchronously works, there must be something wrong with your handling of the asynchronous aspect. It could be:

  • The runloop is not running in NSDefaultRunLoopMode so the connection is unable to schedule itself.
  • Some other part of your code is calling -cancel on the connection before it has a chance to load.
  • You are managing to deallocate the connection before it has a chance to load.

Real problem

Ah, in fact I've just realised what's going on. You are calling:

-[NSApp runModalForWindow:]

Read the description of what this method does. It's not running the run loop like NSURLConnection expects. I'd say that really, you don't want to be presenting a window quite like this while running a URL connection for it.

I'd also suggest that you implement the -connection:didReceiveResponse: delegate method too. You want to check here that the server is returning the expected status code.

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 currentRunLoop] forMode:NSModalPanelRunLoopMode];
[conn start];

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

You say that you're using this in a modal dialog? A modal dialog puts the run loop into a different mode. You should be able to get this to work by scheduling it to run in the modal dialog run loop mode, in addition to the normal run loop mode. Try adding this line of code after you allocate connection in loadURL:

[connection scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSModalPanelRunLoopMode];

Hope that helps.

How do you know it isn't doing anything? Are there any error or warning messages during the compile? Are any error messages showing up on console when the program is running?

Have you tries setting breakpoints in your code and following through what you expect to be happening?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!