Determine Internet Speed ​​Programmatically

I know that you can check if you are connected to the Internet. But is there a way to determine the speed of this connection? I am trying to calculate download speed as well as download speed separately.

How to determine internet speed programmatically?

+8
iphone
source share
2 answers

If you use NSURLConnection to capture a large file (say, 1 MB or more), you can use a delegate to track the progress download progress.

In particular: if you measure the difference in loaded bytes and the time difference between delegate calls, you can calculate the current speed in bytes per second (or another unit of time).

+4
source share

step 1: Take the url file you download and configure it using NSURLSession and its dataTaskWithUrl method.

Step 2: Integrate the NSURLSessionDelegate, NSURLSessionDataDelegate into your controller.

Step 3: Take two CFAbsoluteTime variables that store starTime and assign CFAbsoluteTimeGetCurrent() and a second stopTime in the didReceiveData: delegation method.

Step 4: Copy the speed as follows

  CFAbsoluteTime elapsedTime = stopTime - startTime; float speedOfConnection = elapsedTime != 0 ? [data length] / (stopTime - startTime) / 1024.0 / 1024.0 : -1; 
+2
source share

All Articles