NSURLConnection获取URL

时间:2010-07-22 12:45:10

标签: iphone nsurlconnection

我如何在以下方法中获取URL?

- (void)connectionDidFinishLoading:(NSURLConnection*)theConnection 

6 个答案:

答案 0 :(得分:21)

嘿,有Mihai Damian的评论对我有用:

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

干杯

答案 1 :(得分:9)

应该能够做theConnection.request.URL,但你做不到。烦人,不是吗?

最简单的方法是保存您正在加载的URL(或整个NSURLRequest)。如果您使用多个连接,则可以将它们存储在字典中。请注意-[NSMutableDictionary setObject:forKey:] 复制密钥,NSURLConnections不可复制;解决方法是改为使用CFDictionarySetValue:

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

答案 2 :(得分:4)

当然上述答案有效,我正在寻找类似的解决方案。

刚刚发现NSLog([连接描述]);打印如下:

  

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

因此可以解析[连接描述]返回的字符串,并从连接中获取url,尽管它有点脏。

答案 3 :(得分:3)

您可以像这样获取网址

- (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]);

}

了解更多信息,请阅读here

答案 4 :(得分:3)

这是我的建议

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

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

答案 5 :(得分:0)

在Swift 2.0 iOS 9中,您可以这样做:

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