Note that there are some explanatory texts on larger screens.

plurals
  1. PONSURLConnection didReceiveData not called
    primarykey
    data
    text
    <p>I've read through tons of messages saying the same thing all over again : when you use a NSURLConnection, delegate methods are <em>not called</em>. I understand that Apple's doc are incomplete and reference deprecated methods, which is a shame, but I can't seem to find a solution.</p> <p>Code for the request is there :</p> <pre><code>// Create request NSURL *urlObj = [NSURL URLWithString:url]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:urlObj cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30]; [request setValue:@"gzip" forHTTPHeaderField:@"Accept-Encoding"]; if (![NSURLConnection canHandleRequest:request]) { NSLog(@"Can't handle request..."); return; } // Start connection dispatch_async(dispatch_get_main_queue(), ^{ self.connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES]; // Edited }); </code></pre> <p>...and code for the delegate methods is here :</p> <pre><code>- (void) connection:(NSURLConnection *)_connection didReceiveResponse:(NSURLResponse *)response { NSLog(@"Receiving response: %@, status %d", [(NSHTTPURLResponse*)response allHeaderFields], [(NSHTTPURLResponse*) response statusCode]); self.data = [NSMutableData data]; } - (void) connection:(NSURLConnection *)_connection didFailWithError:(NSError *)error { NSLog(@"Connection failed: %@", error); [self _finish]; } - (void) connection:(NSURLConnection *)_connection didReceiveData:(NSData *)_data { [data appendData:_data]; } - (void)connectionDidFinishDownloading:(NSURLConnection *)_connection destinationURL:(NSURL *) destinationURL { NSLog(@"Connection done!"); [self _finish]; } </code></pre> <p>There's not a lot of error checking here, but I've made sure of a few things :</p> <ul> <li>Whatever happens, <code>didReceiveData</code> is <strong>never</strong> called, so I don't get any data</li> <li>...but the data is transfered (I checked using <code>tcpdump</code>)</li> <li>...and the other methods are called successfully.</li> <li>If I use the <code>NSURLConnectionDownloadDelegate</code> instead of <code>NSURLConnectionDataDelegate</code>, everything works but I can't get a hold on the downloaded file (this is a known bug)</li> <li>The request is not deallocated before completion by bad memory management</li> <li>Nothing changes if I use a standard HTML page somewhere on the internet as my URL</li> <li>The request is kicked off from the main queue</li> </ul> <p>I don't want to use a third-party library, as, ultimately, these requests are to be included in a library of my own, and I'd like to minimize the dependencies. If I have to, I'll use <code>CFNetwork</code> directly, but it will be a huge pain in the you-know-what.</p> <p>If you have any idea, it would help greatly. Thanks!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload