2

i want to know how to get upload speed in swift.

Actually i have this code from Right way of determining internet speed in iOS 8

class SpeedTest: UIViewController, URLSessionDelegate, URLSessionDataDelegate {


    typealias speedTestCompletionHandler = (_ megabytesPerSecond: Double? , _ error: Error?) -> Void

    var speedTestCompletionBlock : speedTestCompletionHandler?

    var startTime: CFAbsoluteTime!
    var stopTime: CFAbsoluteTime!
    var bytesReceived: Int!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        checkForSpeedTest()

    }

    func checkForSpeedTest() {

        testDownloadSpeedWithTimout(timeout: 5.0) { (speed, error) in
            print("Download Speed:", speed ?? "NA")
            print("Speed Test Error:", error ?? "NA")
        }

    }

    func testDownloadSpeedWithTimout(timeout: TimeInterval, withCompletionBlock: @escaping speedTestCompletionHandler) {

        guard let url = URL(string: "https://images.apple.com/v/imac-with-retina/a/images/overview/5k_image.jpg") else { return }

        startTime = CFAbsoluteTimeGetCurrent()
        stopTime = startTime
        bytesReceived = 0

        speedTestCompletionBlock = withCompletionBlock

        let configuration = URLSessionConfiguration.ephemeral
        configuration.timeoutIntervalForResource = timeout
        let session = URLSession.init(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

        if let aTempError = error as NSError?, aTempError.domain != NSURLErrorDomain && aTempError.code != NSURLErrorTimedOut && elapsed == 0  {
            speedTestCompletionBlock?(nil, error)
            return
        }

        let speed = elapsed != 0 ? Double(bytesReceived) / elapsed / 1024.0 / 1024.0 : -1
        speedTestCompletionBlock?(speed, nil)

    }

}

But i think, this code is about download speed (Tell me if i'm wrong).

Sorry i repeat again but how to get the upload speed ?

Best regards

kirusamma
  • 119
  • 11
  • 1
    Well you need to upload something somewhere and measure that. What exactly is the issue? – Joakim Danielson May 27 '19 at 08:59
  • Thanks for your answer. Yes i want to measure that but how ? – kirusamma May 27 '19 at 09:02
  • Your problem is that it is easy to download something of known size; in this case an image from Apple's web site. To determine upload speed you need a server to which you can upload a file of known size; you are unlikely to find such a thing for no cost. – Paulw11 May 27 '19 at 09:13
  • Thanks, i understand. Suppose i can upload a file to a server, the principle is the same as download speed ? To measure the upload speed : ByteReceived / (CurrentTIme - StartTime) / 1024 / 1024, to have my upload speed in MB ? – kirusamma May 27 '19 at 09:31
  • Or maybe there are some API (SpeedTest?) to measure the upload/download speed ? – kirusamma May 27 '19 at 10:02
  • 1
    Can anyone help me to calculate upload speed in Swift? – Anand Gautam Apr 27 '20 at 17:37

0 Answers0