Rony
Rony

Reputation: 1229

NSURLConnection get URL

how i get the URL inside the following method ??

- (void)connectionDidFinishLoading:(NSURLConnection*)theConnection 

Upvotes: 9

Views: 13719

Answers (6)

SNos
SNos

Reputation: 3470

In Swift 2.0 iOS 9 you can do it like:

func connectionDidFinishDownloading(connection: NSURLConnection, destinationURL: NSURL) {
        print(connection.currentRequest.URL!)
    }

Upvotes: 0

Tyler Wall
Tyler Wall

Reputation: 3788

Hey there's a comment from Mihai Damian that worked for me:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSURL *myURL = [[connection currentRequest] URL];

Cheers

Upvotes: 21

Grant Fong
Grant Fong

Reputation: 311

Here is my suggestion

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    self.rssFeedConnection = nil;

    NSLog(@"connectionDidFinishLoading url : %@ ", connection.originalRequest.URL);
}

Upvotes: 3

b123400
b123400

Reputation: 6118

Of course the above answers work, and I am looking for similar solution.

Just found that NSLog([connection description]); prints something like:

< NSURLConnection: 0x9129520, http://google.com>

So it is possible to parse the string returned by [connection description], and get the url from the connection, though it is kind of dirty.

Upvotes: 4

Viktor Apoyan
Viktor Apoyan

Reputation: 10755

You can get URL like this

- (void)connection:(NSURLConnection *)connection

  didFailWithError:(NSError *)error

{

    // release the connection, and the data object

    [connection release];

    // receivedData is declared as a method instance elsewhere

    [receivedData release];



    // inform the user

    NSLog(@"Connection failed! Error - %@ %@",

          [error localizedDescription],

          [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);

}

for more information you van read here.

Upvotes: 3

tc.
tc.

Reputation: 33592

You ought to be able to do theConnection.request.URL, but you can't. Annoying, isn't it?

The simplest way is to just save the URL (or the whole NSURLRequest) that you were loading. If you're using multiple connections, you can store them in a dictionary. Note that -[NSMutableDictionary setObject:forKey:] copies keys, and NSURLConnections are not copyable; the workaround is to use CFDictionarySetValue instead:

CFDictionarySetValue((CFMutableDictionaryRef)dict, connection, request); 

Upvotes: 9

Related Questions