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

2.3k views Asked by At

I'm currently working on an app that determine's the user's internet speed connection based on how quickly the file is downloaded.

I found a similar question in which @Rob provided the answer in Swift: Right way of determining internet speed in iOS 8

Currently I'm using Swift 3, and have tried to convert his code from an older Swift as follows:

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

func testDownloadSpeedWithTimout(timeout: TimeInterval, completionHandler:@escaping (_ megabytesPerSecond: Double?, _ error: NSError?) -> ())
{

    let url = URL(string: "http://www.someurl.com/file")!

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

    let configuration = URLSessionConfiguration.default
    configuration.timeoutIntervalForResource = timeout

    let session = URLSession(configuration: configuration, delegate: self, delegateQueue: nil)
    session.dataTask(with: 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?.domain == NSURLErrorDomain && error?.code == NSURLErrorTimedOut)) else {

        speedTestCompletionHandler(megabytesPerSecond: nil, error: error)
        return
    }

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

I managed to convert most of the code, but I seem to be getting some errors2:

enter image description here

Can someone provide assistance with this? Thanks

2

There are 2 answers

0
Pragnesh Vitthani On BEST ANSWER

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)
}
}
0
FelixSFD On

Error 1

Error is a protocol in Swift 3. It's the ErrorType from Swift 2. The property domain is part of NSError.

I'm not 100% sure if the URLSessionTask always passes a NSError in the didCompleteWithError function, but I think so. You could try casting error as NSError and see if it succeeds.

Error 2 and 3

The error message seems misleading to me. But when I had a look at the declaration of speedTestCompletionHandler, I noticed, that you must not use the argument labels. _ in front of a function parameter means, that you have to leave out the parameter label. Instead you have to call speedTestCompletionHandler() like this:

speedTestCompletionHandler(nil, error)
...
speedTestCompletionHandler(speed, nil)