Right Way of Determining Internet Speed in iOS 8

What is the best way to make download speed test app using swift3?

I put my file in different servers and each one give me different result so the problem is the speed result depends on the server the you download from and the solution is to have file in multi servers and detect the best server for the client and download from it.

Determine the speed on internet programmatically

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

Swift 3: How to determine a downloaded file's internet speed?

You should replace your code with this:

class ViewController: UIViewController, URLSessionDelegate, URLSessionDataDelegate
{

override func viewDidLoad()
{
super.viewDidLoad()

testDownloadSpeedWithTimout(timeout: 5.0) { (megabytesPerSecond, error) -> () in
print("\(megabytesPerSecond); \(error)")
}
}

var startTime: CFAbsoluteTime!
var stopTime: CFAbsoluteTime!
var bytesReceived: Int!
var speedTestCompletionHandler: ((_ megabytesPerSecond: Double?, _ error: NSError?) -> ())!

/// Test speed of download
///
/// Test the speed of a connection by downloading some predetermined resource. Alternatively, you could add the
/// URL of what to use for testing the connection as a parameter to this method.
///
/// - parameter timeout: The maximum amount of time for the request.
/// - parameter completionHandler: The block to be called when the request finishes (or times out).
/// The error parameter to this closure indicates whether there was an error downloading
/// the resource (other than timeout).
///
/// - note: Note, the timeout parameter doesn't have to be enough to download the entire
/// resource, but rather just sufficiently long enough to measure the speed of the download.

func testDownloadSpeedWithTimout(timeout: TimeInterval, completionHandler:@escaping (_ megabytesPerSecond: Double?, _ error: NSError?) -> ()) {
let url = NSURL(string: "http://www.someurl.com/file")!

startTime = CFAbsoluteTimeGetCurrent()
stopTime = startTime
bytesReceived = 0
speedTestCompletionHandler = completionHandler

let configuration = URLSessionConfiguration.ephemeral
configuration.timeoutIntervalForResource = timeout
let session = URLSession(configuration: configuration, delegate: self, delegateQueue: nil)
session.dataTask(with: url as URL).resume()
}

func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data)
{
bytesReceived! += data.count
stopTime = CFAbsoluteTimeGetCurrent()
}

func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?)
{
let elapsed = stopTime - startTime
guard elapsed != 0 && (error == nil || ((error! as NSError).domain == NSURLErrorDomain && error?._code == NSURLErrorTimedOut)) else{
speedTestCompletionHandler(nil, error as? NSError)
return
}

let speed = elapsed != 0 ? Double(bytesReceived) / elapsed / 1024.0 / 1024.0 : -1
speedTestCompletionHandler(speed, error as? NSError)
}
}


Related Topics



Leave a reply



Submit