Recently, due to the need to do network detection, I began to investigate how to obtain various contents of the request in iOS. Such as first packet time, DNS resolution time, connect request time, SSL handshake time, download speed, upload speed and other network indicators. These data cannot be obtained by using native methods of iOS. IOS 10 can obtain such information by using the following methods:

- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task DidFinishCollectingMetrics: (NSURLSessionTaskMetrics *) metrics API_AVAILABLE (macosx (10.12), the ios (10.0), watchos (3.0), Tvos (10.0));Copy the code

But if it’s iOS9 or below, it’s not going to work, so you have to keep thinking about it. I had another research task that required libcurl, so I tried it out and found a solution!

Curl_easy_getinfo libcurl curl_easy_getinfo libcurl curl curl_easy_getinfo https://curl.haxx.se/libcurl/c/curl_easy_getinfo.html there are detailed introduction, what do you want to get parameters of sample points in the Example code, very simple. Here is an Example of how to obtain DNS resolution time:

curl = curl_easy_init();
if(curl) {
  double namelookup;
  curl_easy_setopt(curl, CURLOPT_URL, url);
  res = curl_easy_perform(curl);
  if([CURLE_OK](https://curl.haxx.se/libcurl/c/libcurl-errors.html#CURLEOK) == res) {
    res = curl_easy_getinfo(curl, CURLINFO_NAMELOOKUP_TIME, &namelookup);
    if([CURLE_OK](https://curl.haxx.se/libcurl/c/libcurl-errors.html#CURLEOK) == res) {
      printf("Time: %.1f", namelookup);
    }
}
/* always cleanup */
curl_easy_cleanup(curl);

Copy the code

Simple, the rest of the network request parameters you want to obtain to see the official website sample bar, all kinds of detection SDK, such as listening cloud, friendship will also be obtained through this? 🙂