0

I need to get the current request's connect server IP address when I use NSURLSession. What are the methods available?

I am trying to use

-(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didFinishCollectingMetrics:(NSURLSessionTaskMetrics *)metrics

but it doesn’t seem to have the target IP address.

Supplement for How can I get the exactly ip address which used to connect in iOS?

My demand is to monitor network performance in an existing project ,so not be able to replace NSURLsesion with CFNetwork. Or can anyone tell me how to hook or use the lower level direction?

Thanks in advance !

  • More like you need NSURL to IP address translation? If so you have to go low level and use CFNetServiceGetAddressing. – Marek H Feb 19 '19 at 08:59
  • @MarekH thanks for your answer,but What I need to get is the IP used by the **current request** and I can't do HTTPDNS. Because DNS can correspond to multiple IPs, simple transitions can't meet demand. – daybreak zhou Feb 21 '19 at 06:47
  • In this case you need to go lower level and use CFNetwork. This can't be done using NSURLSession – Marek H Feb 21 '19 at 08:58
  • https://stackoverflow.com/questions/31508871/how-can-i-get-the-exactly-ip-address-which-used-to-connect-in-ios – Marek H Feb 21 '19 at 09:05
  • Possible duplicate of [How can I get the exactly ip address which used to connect in iOS?](https://stackoverflow.com/questions/31508871/how-can-i-get-the-exactly-ip-address-which-used-to-connect-in-ios) – Marek H Feb 21 '19 at 09:05
  • @MarekH The goal of getting IP is consistent with it, and the difference has been added. Thank you again for your reply. – daybreak zhou Feb 22 '19 at 03:31
  • If you want to try search github.com for mentioned functions of cfnetwork. Asking on stackoverflow you are required to show some coding effort. People don't like to see what you just did. – Marek H Feb 22 '19 at 08:40

1 Answers1

0

I have collected this data in

 -(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didFinishCollectingMetrics:(NSURLSessionTaskMetrics *)metric

Unfortunately, it is a private api.

code is:

SEL selector = NSSelectorFromString(@"_timingData");

NSDictionary *timingData = nil;
if ([task respondsToSelector:selector]) {
    timingData = [task performSelector:selector];
}
NSString *remoteServerIP = @"";
if (timingData && [timingData isKindOfClass:[NSDictionary class]]) {
    NSString *key = @"_kCFNTimingDataRemoteAddressAndPort";
    remoteServerIP = timingData[key];
}