我如何获得以下方法中的URL?NSURLConnection获取URL
- (void)connectionDidFinishLoading:(NSURLConnection*)theConnection
我如何获得以下方法中的URL?NSURLConnection获取URL
- (void)connectionDidFinishLoading:(NSURLConnection*)theConnection
您应该能够做到theConnection.request.URL
,但你不能。讨厌,不是吗?
最简单的方法是保存您加载的URL(或整个NSURLRequest)。如果您使用多个连接,则可以将它们存储在字典中。请注意0复制密钥和NSURLConnections不可复制;解决办法是使用CFDictionarySetValue代替:
CFDictionarySetValue((CFMutableDictionaryRef)dict, connection, request);
你可以得到URL这样
- (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。
当然,上述答案的工作,我正在寻找类似的解决方案。
刚发现NSLog([connection description]);印像:
< NSURLConnection的:0x9129520,http://google.com>
因此,有可能解析通过[连接描述]返回的字符串,然后从连接获得url,虽然它是一种脏。
这将不再适用于iOS 5;你可以使用NSURLConnection的currentRequest而不是 – 2012-01-16 11:52:50
@MihaiDamian你知道iOS5的一个可比较的东西。我的程序工作良好,直到我升级到iOS5,现在需要另一种方式从NSURLConnection获取URL – MikeIsrael 2012-04-24 08:11:22
**从不** **依赖于“-description”的输出,除非它专门记录为使用特定格式 – 2013-07-25 09:47:46
这里是我的建议
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
self.rssFeedConnection = nil;
NSLog(@"connectionDidFinishLoading url : %@ ", connection.originalRequest.URL);
}
在雨燕2.0的iOS 9,你可以不喜欢它
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSURL *myURL = [[connection currentRequest] URL];
干杯:
func connectionDidFinishDownloading(connection: NSURLConnection, destinationURL: NSURL) {
print(connection.currentRequest.URL!)
}
好的......谢谢... t c ... – Rony 2010-07-22 18:15:26