GithubHelp home page GithubHelp logo

rldaulton / connectedness Goto Github PK

View Code? Open in Web Editor NEW
39.0 3.0 4.0 4 KB

a simple and easy way to check network connection and strength in iOS using Swift

License: MIT License

Swift 100.00%
swift networking wifi lte

connectedness's Introduction

connectedness

Check network connection and speed with a single method call. connectedness first checks your access to the network (wifi or mobile 1X/3G/LTE), then determines your network's speed by loading a dummy resource.

It checks this network speed with a default timeout of 5 seconds, then compares your speed to the approximate national average 3G network - if it is slower than this average, an error is thrown for you to handle as you please.

Requirements

  • Xcode 7 or higher
  • iOS 8.0 or higher
  • Swift 2.3

Installation

Manual Only

Add the connectedness.swift file into your project.

Usage

1.) Determine Resource

In order to check the speed of your network, a dummy resource must be downloaded from somewhere online.

Replace the link in the testDownloadSpeedWithTimout...() function with a link to your own resource. A random .txt file will do.

public func testDownloadSpeedWithTimout(timeout: NSTimeInterval, completionHandler:(megabytesPerSecond: Double?, error: NSError?) -> ()) {
        let url = NSURL(string: "http://insert.your.site.here/yourfile")!
...

2.) Determine what you want your network speed to be compared against by changing the value associated with the if megabytesPerSecond!>= ... {. Also, change the number associated with testDownloadSpeedWithTimout(...) to increase or decrease the timeout time, in seconds. See below.

connectedSpeed().testDownloadSpeedWithTimout(5.0) { (megabytesPerSecond, error) -> () in
        if ((error == nil) && isReachable && !needsConnection){
            if megabytesPerSecond!>=0.05{
 ...

The default 0.05 measures out approximately to the national average 3G network. Lower this number to compare against slower speeds, and raise it to compare against higher speeds. This is most useful when you may want to tell users that their slow speed will negatively affect their user experience and tell them to try for a better signal.

For example, a value of 1.1 equates to a wifi connection speed of around 35 Mbps. These numbers are not exact.

3.) Simply call the connectedToNetwork() from anywhere in your project. Connection will be determined first, then speed will be measured.

func viewDidLoad(){
  connectedToNetwork()
}

Debugger prints your connectivity nicely

       | Connected |
=======Network Speed=======
Mbps: 1.00830526622741
error: nil
=======-------------=======

Author

Ryan Daulton

License

connectedness is available under the MIT license. See the LICENSE file for more info.

connectedness's People

Contributors

rldaulton avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

connectedness's Issues

Swift4

Is this code usable for swift 4?

reference speed variable elsewhere

I am utilizing the function as an external "connectedness.swift" by calling it in my main ViewController. How can i call the integer variable "speed" within my ViewController rather than just printing out my speed when the function is called? I am trying to create a log of the speed at any given time interval and concatenate into an array. Thank you.

0.05 and 1.1 measurements?

Hi, can you tell me where you got the 0.05 measurements to 3G network and 1.1 to the equivalent 35 Mbps?

With the default 0.05 set, I see the Mbps output of 0.1754, but speed test shows I have a download speed of 67 Mbps so I'm not sure how to relate the two?

Thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.