I have looked at NSURLConnectionDelegate connection:didReceiveData not working already, but there didn't seem to be any good result from that, so I am curious why I am not able to get any data.
I put in breakpoints in didReceiveResponse
and didReceiveData
.
It does print out "connection succeeded", so I know that the connection is started.
I am using ARC for memory management.
- (void)load {
request = [NSMutableURLRequest requestWithURL:myURL
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:60];
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (conn) {
[conn start];
NSLog(@"connection succeeded, %s", [myURL description]);
responseData = [NSMutableData data];
} else {
NSLog(@"connection failed");
}
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
responseData = [[NSMutableData alloc] init];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[responseData appendData:data];
}
UPDATE:
To see how I test this look at Asynchronous unit test not being called by SenTestCase.
I did implement the two methods mentioned by jonkroll, in his answer, I just didn't show them, but, they also aren't being called.
I had added [conn start]
only because it wasn't working, and I was hoping that may solve it, but no such luck.
When you declare your connection like this:
You are creating a local pointer. When your method completes, since it was the last strong reference to the
NSURLConnection
,ARC
releases it. You need to use a strong ivar (and/or) property to hold a strong reference to theNSURLConnection
you create.Edit
Here is basic sample of code that I tested in a sample project. Give it a run. Verbose logging helps.
The NSURLConnection method
initWithRequest
starts an asynchronous request for data from a url. Because the request is done asynchronously you can't expect to work with the response in the same method in which the request is invoked. Instead you need to do so in the NSURLConnection's delegate callback methods. You have already implementeddidReceiveResponse:
anddidReceiveData:
, but there are a couple others that will be useful to you.If you want to look at the contents of the response you should do so in
connectionDidFinishLoading:
The fact that your code prints out "connection succeeded" doesn't really mean that the request was successful, only that the NSURLConnection object was created successfully. To test whether there was a problem with the connection you can implement the delegate method
connection:didFailWithError:
Also there is no need to call
[conn start]
. The request will be started automatically when you callinitWithRequest:
I suggest reading Apple's documentation on Using NSURLConnection for more details.