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 sp
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;
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
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).