title: iPhone NSURLConnection: connectionDidFinishLoading - how to return a string to the calling method
body: <p>I have reviewed similar stackoverflow questions/answers to this but I am still stumped.</p>
  
  <p>I'm a beginner and I'm really struggling with this. With the iPhone, I can download XML from a URL but I cannot store the result string in a NSString variable and see it from the calling function.</p>
  
  <p>I have the following declarations in a custom made class:</p>
  
  <pre><code>@interface comms : NSObject {
      NSString *currURL, *receivedString;
      NSMutableData *receivedData;
      NSURLConnection *conn;
  }
  
  @property (copy, readwrite) NSString *currURL;
  @property (copy, readonly) NSString *receivedString;
  @property (nonatomic, assign) NSMutableData *receivedData;
  @property (nonatomic, assign) NSURLConnection *conn;
  
  -(void) getContentURL;
  </code></pre>
  
  <p>I have the following method in the comms class:</p>
  
  <pre><code>-(void) getContentURL
  {
      NSLog( @"Begin getContentURL." );
  
      // Request data related.
      NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] 
                                      autorelease];
      [request setURL:[NSURL URLWithString: currURL]];
  
      // Content-Type related.
      [request setValue:@"application/x-www-form-urlencoded" 
     forHTTPHeaderField:@"Content-Type"];
  
      // Create Connection.
      conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
  
      if (conn) {
          // The connection was established.
          receivedData = [[NSMutableData data] retain];
          NSLog( @"Data will be received from URL: %@", request.URL );
      }
      else
      {
          // The download could not be made.
          NSLog( @"Data could not be received from: %@", request.URL );
      }
  
      // PROBLEM - receivedString is NULL here.
      NSLog( @"From getContentURL: %@", receivedString );
  }
  </code></pre>
  
  <p>I have created the required delegates in the comms class as per the following:</p>
  
  <pre><code>-(void)connection:(NSURLConnection *)connection didReceiveResponse:
      (NSURLResponse *)response
  {
      // Discard all previously received data.
      [receivedData setLength:0];
  }
  
  -(void)connection:(NSURLConnection *)connection didReceiveData:
  (NSData *)data
  {
      // Append the new data to the receivedData.
  [receivedData appendData:data];     
  }
  
  -(void)connectionDidFinishLoading:(NSURLConnection *)connection
  {
      // Connection succeeded in downloading the request.
      NSLog( @"Succeeded! Received %d bytes of data", [receivedData length] );
  
      // Convert received data into string.
      receivedString = [[NSString alloc] initWithData:receivedData 
          encoding:NSASCIIStringEncoding];
      NSLog( @"From connectionDidFinishLoading: %@", receivedString );
  
      // release the connection, and the data object
      [conn release];
      [receivedData release];
  }
  </code></pre>
  
  <p>I can successfully output the receivedString string using NSLog in the connectionDidFinishLoading delegate.</p>
  
  <pre><code>    // Convert received data into string.
      receivedString = [[NSString alloc] initWithData:receivedData 
          encoding:NSASCIIStringEncoding];
      NSLog( @"From connectionDidFinishLoading: %@", receivedString );
  </code></pre>
  
  <p>However, when I output the receivedString string in the getContentURL it's null (and hence is also null from the ViewController.m class which I call the comms class from).</p>
  
  <pre><code>    // PROBLEM - receivedString is NULL here.
      NSLog( @"From getContentURL: %@", receivedString );
  </code></pre>
  
  <p>Any ideas on how I can see the value of receivedString in getContentURL and from the ViewController.m class?</p>