performance - How to check the network speed using swift -


i have googled many pages saying on network reachability (only yes or no availibilty), never heard detect network speed using swift xcode environment. need feature(detect network speed host), gave me clue on issue

i made func calculate network speed in swift 3. download image server , calculate elapsed time.

 func testspeed()  {       let url = url(string: "http://my_image_on_web_server.jpg")        let request = urlrequest(url: url!)      let session = urlsession.shared              let starttime = date()      let task =  session.datatask(with: request) { (data, resp, error) in          guard error == nil && data != nil else{              print("connection error or data nill")              return         }          guard resp != nil else{              print("respons nill")             return         }               let length  = cgfloat( (resp?.expectedcontentlength)!) / 1000000.0              print(length)            let elapsed = cgfloat( date().timeintervalsince(starttime))          print("elapsed: \(elapsed)")          print("speed: \(length/elapsed) mb/sec")       }       task.resume()   } 

Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -