NSURLConnection fails after upgrade to iOS 5

2019-09-17 10:28发布

问题:

I have a very simple NSURLConnection call that works perfectly in all iOS versions except iOS 5. Since this is a 'sendSynchronousRequest' call, there are no NSURL delegates declared anywhere in the app (the response should come directly back to this method call). Also, because this is a sendSynchronousRequest, there are no 'didReceiveData' or other NSURL-associated methods implemented in the app.

Here is the offending line of code:

NSData *response = [NSURLConnection sendSynchronousRequest: serviceRequest returningResponse:nil error:nil];

When I step through the code in the debugger, I can confirm that app is sending the request, and that the server is receiving the request. I can also confirm that the server is then sending a response back to the client.

This was all working perfectly until I upgraded to iOS 5. Now, after the update to iOS5, the NSData variable (response) is never receiving anything and always comes back with 0 bytes.

Other than the update to iOS5, there have been no code changes at all.

回答1:

There were several changes made to the NSURLConnection class in iOS 5. In particular, several delegate methods that were deprecated. I experienced a similar issue and it was caused by one of the delegate methods no longer being called. If this sounds like your issue, take a look at the formal delegate protocols NSURLConnectionDelegate and NSURLConnectionDataDelegate.



回答2:

I had the same problem using a custom Web server. The problem turned out to be an invalid header in the response. I changed:

HTTP/1.1 200/OK

to:

HTTP/1.1 200 OK

iOS 5 now accepts the response. Check your packet capture. You may have the same or similar problem.



标签: xcode ios5