开发者

NSURL from NSURLConnection?

开发者 https://www.devze.com 2022-12-24 12:17 出处:网络
It seems dead simple, as to create an NSURLConnection I usually do this: NSURL *theURL = [NSURL URLWithString:urlString];

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 myse开发者_StackOverflow社区lf (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?


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
}
0

精彩评论

暂无评论...
验证码 换一张
取 消