You code snippet was taken from this answer. I’ve updated it to use URLSession
. The Swift rendition is below:
class ViewController: UIViewController {
var startTime: CFAbsoluteTime!
var bytesReceived: Int = 0
var speedTestCompletionHandler: ((Result<Double, Error>) -> Void)?
override func viewDidLoad() {
super.viewDidLoad()
testDownloadSpeed(timeout: 5) { result in
switch result {
case .failure(let error): print(error)
case .success(let megabytesPerSecond): print(megabytesPerSecond)
}
}
}
/// 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 testDownloadSpeed(timeout: TimeInterval, completionHandler: @escaping (Result<Double, Error>) -> Void) {
let url = URL(string: "https://insert.your.site.here/yourfile")!
startTime = CFAbsoluteTimeGetCurrent()
bytesReceived = 0
speedTestCompletionHandler = completionHandler
let configuration = URLSessionConfiguration.ephemeral
configuration.timeoutIntervalForResource = timeout
let session = URLSession(configuration: configuration, delegate: self, delegateQueue: nil)
session.dataTask(with: url).resume()
session.finishTasksAndInvalidate()
}
}
extension ViewController: URLSessionDataDelegate {
func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {
bytesReceived += data.count
}
}
extension ViewController: URLSessionDelegate {
func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
let stopTime = CFAbsoluteTimeGetCurrent()
let elapsed = stopTime - startTime
guard elapsed != 0 && (error == nil || (error as? URLError)?.code == .timedOut) else {
speedTestCompletionHandler?(.failure(error ?? URLError(.unknown)))
return
}
let speed = elapsed != 0 ? Double(bytesReceived) / elapsed / 1024.0 / 1024.0 : -1
speedTestCompletionHandler?(.success(speed))
}
}