12

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

How to determine the speed on internet programmatically?

Milan Gupta
  • 1,181
  • 8
  • 21
Shishir.bobby
  • 10,994
  • 21
  • 71
  • 100
  • 2
    It doesn't answer your question, but as an aside, in my mind an app should be able to support whatever speeds are available in practice, as people connect the same device to multiple internet sources (WiFi at home or work, 3G out and about); and even with a single source speeds in practice can rapidly change. – Jim Blackler Feb 29 '12 at 09:07
  • Is it possible now to measure internet speed or wifi strength. – Imran Oct 13 '14 at 12:47

3 Answers3

5

If you use NSURLConnection to grab a large file (say, 1 MB or greater), you can use a delegate to track intermediate download progress.

Specifically: If you measure the difference in bytes downloaded and the difference in time between calls to the delegate, then you can calculate the ongoing speed in bytes per second (or other time unit).

Alex Reynolds
  • 95,983
  • 54
  • 240
  • 345
  • 2
    NB: If your app tries to download >1MB on my 3G connection, just to determine that it's *slooooow*, I won't be happy (my carrier might be, though). In fact, I'll be violently unhappy that you're pointlessly eating away at my meager data limit; even more if you do that repeatedly. (If you're measuring data transfer(s) that the app actually needs - it will take a while, but I'll manage) – Piskvor left the building Feb 29 '12 at 09:16
  • 1
    Oh, for sure. But this could provide an ongoing speed of normal downloads that go on in the course of the app doing its usual business, for example. Or if the app is a network test app, then the end user would run this knowing that it will request a large file over the network connection. So I'd argue it's still a useful approach even for cellular connections, if used intelligently and appropriately... – Alex Reynolds Feb 29 '12 at 09:37
  • Is it possible to determine internet speed without downloading any file? – Harsha Jun 06 '19 at 11:01
  • Via ping/ICMP, maybe? https://stackoverflow.com/questions/4575537/calculate-upload-download-speed-by-ping – Alex Reynolds Jun 06 '19 at 16:14
3

step 1: Take downloadable file url and configure a it with a NSURLSession and its method dataTaskWithUrl.

step 2 : Integrate NSURLSessionDelegate, NSURLSessionDataDelegate method in your controller.

step 3: Take two CFAbsoluteTime variable which store starTime and assign CFAbsoluteTimeGetCurrent() and second one stopTime in didReceiveData: Delegate method.

step 4 : Count speed like this

  CFAbsoluteTime elapsedTime = stopTime - startTime;
  float speedOfConnection = elapsedTime != 0 ? [data length] / (stopTime - startTime) / 1024.0 / 1024.0 : -1;
byJeevan
  • 3,728
  • 3
  • 37
  • 60
0

There are 2 main ways to calculate download/upload speed.

Passive testing - This is done by using iOS methods which give you currently transferred bytes. You poll this frequently and calculate speed by transferred bytes divided by time. This method will give you speed which is more closer to the actual user experience. However, it will not provide you with capacity measurement - i.e what is the expected capacity of the connection. For example for fixed networks ISPs usualy sell packages based on speed - e.g. 100 Mbit package, 1 Gbit package. If you want to see if that ISP is delivering the speed, Passive approach is not the way! The speeds will be much lower as users are not using full capacity all the time.

Active testing - this method requires downloading and uploading data to the remote server to get the download/upload speed and latency. This is what previous commenters here suggested. The important way is to realize if you want to test using single thread or multiple threads. Single thread will not saturate the internet connection and will not show you maximum capacity of the connection.

There are many methodologies how to test "internet speed", there is no one "true" speed. You can check following standards to give you more idea what is the recommended way:

https://itu.int/en/ITU-T/C-I/Pages/IM/Internet-speed.aspx

https://itu.int/itu-t/recommendations/rec.aspx?rec=q.3960

https://itu.int/ITU-T/recommendations/rec.aspx?rec=14125

https://tools.ietf.org/pdf/rfc6349.pdf

You can also use our iOS SDK which should do what you need and is compliant with ITU standard:

https://github.com/speedchecker/speedchecker-sdk-ios

Janusz
  • 167
  • 2
  • 12