Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I've managed to do this by placing the sendAsynchronousRequest method in a separate <code>DownloadWrapper</code> class, as follows:</p> <pre><code>// // DownloadWrapper.h // // Created by Ahmed Khalaf on 16/12/11. // Copyright (c) 2011 arkuana. All rights reserved. // #import &lt;Foundation/Foundation.h&gt; @protocol DownloadWrapperDelegate - (void)receivedData:(NSData *)data; - (void)emptyReply; - (void)timedOut; - (void)downloadError:(NSError *)error; @end @interface DownloadWrapper : NSObject { id&lt;DownloadWrapperDelegate&gt; delegate; } @property(nonatomic, retain) id&lt;DownloadWrapperDelegate&gt; delegate; - (void)downloadContentsOfURL:(NSString *)urlString; @end @implementation DownloadWrapper @synthesize delegate; - (void)downloadContentsOfURL:(NSString *)urlString { NSURL *url = [NSURL URLWithString:urlString]; NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:TIMEOUT_INTERVAL]; NSOperationQueue *queue = [[NSOperationQueue alloc] init]; [NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { if ([data length] &gt; 0 &amp;&amp; error == nil) [delegate receivedData:data]; else if ([data length] == 0 &amp;&amp; error == nil) [delegate emptyReply]; else if (error != nil &amp;&amp; error.code == ERROR_CODE_TIMEOUT) [delegate timedOut]; else if (error != nil) [delegate downloadError:error]; }]; } @end </code></pre> <p>To utilise this class, I do the following, in addition to declaring the <code>DownloadWrapper *downloadWrapper</code> variable (in the interface declaration) and implementing the protocol methods which handles the response or a lack of one:</p> <pre><code>NSString *urlString = @"http://yoursite.com/page/to/download.html"; downloadWrapper = [DownloadWrapper alloc]; downloadWrapper.delegate = self; [downloadWrapper downloadContentsOfURL:urlString]; </code></pre> <p>Then I simply do the following to 'cancel' the connection when the view is about to disappear:</p> <pre><code>- (void)viewDidUnload { [super viewDidUnload]; downloadWrapper = nil; } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; [downloadWrapper setDelegate:nil]; } </code></pre> <p>It's as simple as that. This would hopefully mimic the documented <code>cancel</code> method, which states that it does the following:</p> <blockquote> <p>Once this method is called, the receiver’s delegate will no longer receive any messages for this NSURLConnection.</p> </blockquote> <p>I was concerned that this (somewhat naive) method means that the packets of data would still come through in response to our URL request - only that we're no longer 'listening in' as the delegate. But then I realised that once the URL request was sent through, there's really no way of stopping the response from coming back to us - we can only disregard it (if not at this level, then still at some lower level in the network hierarchy). Please correct me if I'm wrong.</p> <p>Either way, hope this helps.</p>
    singulars
    1. This table or related slice is empty.
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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