4

It seems dead simple, as to create an NSURLConnection I usually do this:

NSURL *theURL = [NSURL URLWithString:urlString];
NSURLRequest *req = [NSURLRequest requestWithURL:theURL];
NSURLConnection *connection = [NSURLConnection connectionWithRequest:req delegate:self];

But how can I get the URL back in the delegate methods? Short of hanging on to them myself (I'm running many connections at once, so this becomes slightly messy). It seems as though I should be able to get the URL back from a connection.

Am I missing something?

Mike Abdullah
  • 14,933
  • 2
  • 50
  • 75
jbrennan
  • 11,943
  • 14
  • 73
  • 115
  • 1
    did you ever figured it out to get url in `- (void)connectionDidFinishLoading:(NSURLConnection *)connection` with `NSURLConnection` object? – Kapil Choubisa Sep 07 '11 at 09:58

1 Answers1

9

In -connection:didReceiveResponse: you can get the URL. Note that this may not be the same URL you created the connection with since the connection may have been redirected.

- (void)connection:(NSURLConnection *)connection 
            didReceiveResponse:(NSURLResponse *)response {
    NSURL * url = [response URL]; // The URL
}
Cory Kilger
  • 13,034
  • 3
  • 33
  • 24
  • thats ok for showing initialization, but how to know which connection finished first from - (void)connectionDidFinishLoading:(NSURLConnection *)connection – illuminatus Jul 06 '11 at 07:26
  • 2
    I'm not sure exactly what you're saying, but it sounds like you're using a single delegate for multiple connections. I don't recommend that since you'll often set instance variables based on the responses and data received in the delegate methods. You should probably create a class for the sole purpose of being a delegate and instantiate a new one for each connection. – Cory Kilger Jul 14 '11 at 18:25