Aucune description

Ashley Mills e2bdc2ab89 Merge pull request #338 from teleportr-app/bugfix/dealloc-race-condition il y a 6 ans
Reachability.xcodeproj accb6d78cb Move swift version to 5.0 il y a 6 ans
ReachabilityAppleTVSample 51b064ec79 Add try? to sample app. Remove development team il y a 6 ans
ReachabilityMacSample 51b064ec79 Add try? to sample app. Remove development team il y a 6 ans
ReachabilitySample 51b064ec79 Add try? to sample app. Remove development team il y a 6 ans
Sources 45a7d3c0e8 Merge branch 'master' into bugfix/dealloc-race-condition il y a 6 ans
Tests b2d6434fa5 Improve error handling and allow configuring notification queue il y a 6 ans
.gitignore ca1081d13c Added Carthage/Build to .gitignore il y a 9 ans
CHANGELOG.md 1ce14e6b9a Update CHANGELOG.md il y a 6 ans
CONTRIBUTING.md b9b300d0f5 Update CONTRIBUTING.md il y a 7 ans
LICENSE 5e1e23e0ba Year updated in the License File il y a 9 ans
Package.swift 0dc49b114d Fix SPM il y a 7 ans
README.md de47ef3beb Update README.md il y a 6 ans
ReachabilitySwift.podspec 6c8652a8ea Add Swift version to podspec il y a 6 ans

README.md

Reachability.swift

Reachability.swift is a replacement for Apple's Reachability sample, re-written in Swift with closures.

It is compatible with iOS (8.0 - 12.0), OSX (10.9 - 10.14) and tvOS (9.0 - 12.0)

Inspired by https://github.com/tonymillion/Reachability

Supporting Reachability.swift

Keeping Reachability.swift up-to-date is a time consuming task. Making updates, reviewing pull requests, responding to issues and answering emails all take time.

If you're an iOS developer who's looking for a quick and easy way to create App Store screenshots, please try out my app Screenshot Producer

Devices  Layout Copy Localize Export      

And don't forget to the repo. This increases its visibility and encourages others to contribute.

Thanks Ash

IMPORTANT

Version 4.0 breaking changes

CocoaPods:

If you're adding Reachability.swift using CocoaPods, note that the framework name has changed from ReachabilitySwift to Reachability (for consistency with Carthage)

Previously:

enum NetworkStatus {
    case notReachable, reachableViaWiFi, reachableViaWWAN
}
var currentReachabilityStatus: NetworkStatus

Now:

enum Connection {
    case none, wifi, cellular
}
var connection: Connection

Other changes:

  • reachableOnWWAN has been renamed to allowsCellularConnection

  • reachability.currentReachabilityString has been deprecated. Use "\(reachability.connection)" instead.

  • isReachable has been deprecated. Use connection != .none instead.

  • isReachableViaWWAN has been deprecated. Use connection == .cellular instead.

  • The notification for reachability changes has been renamed from ReachabilityChangedNotification to Notification.Name.reachabilityChanged

  • All closure callbacks and notification are fired on the main queue (including when startNotifier() is called)

Got a problem?

Please read https://github.com/ashleymills/Reachability.swift/blob/master/CONTRIBUTING.md before raising an issue.

Installation

Manual

Just drop the Reachability.swift file into your project. That's it!

CocoaPods

CocoaPods is a dependency manager for Cocoa projects. To install Reachability.swift with CocoaPods:

  1. Make sure CocoaPods is installed.

  2. Update your Podfile to include the following:

    use_frameworks!
    pod 'ReachabilitySwift'
    
  3. Run pod install.

  4. In your code import Reachability like so: import Reachability

Carthage

Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks. To install Reachability.swift with Carthage:

  1. Install Carthage via Homebrew

    $ brew update
    $ brew install carthage
    
  2. Add github "ashleymills/Reachability.swift" to your Cartfile.

  3. Run carthage update.

  4. Drag Reachability.framework from the Carthage/Build/iOS/ directory to the Linked Frameworks and Libraries section of your Xcode project’s General settings.

  5. Add $(SRCROOT)/Carthage/Build/iOS/Reachability.framework to Input Files of Run Script Phase for Carthage.

  6. In your code import Reachability like so: import Reachability

Example - closures

NOTE: All closures are run on the main queue.

//declare this property where it won't go out of scope relative to your listener
let reachability = Reachability()!

reachability.whenReachable = { reachability in
    if reachability.connection == .wifi {
        print("Reachable via WiFi")
    } else {
        print("Reachable via Cellular")
    }
}
reachability.whenUnreachable = { _ in
    print("Not reachable")
}

do {
    try reachability.startNotifier()
} catch {
    print("Unable to start notifier")
}

and for stopping notifications

reachability.stopNotifier()

Example - notifications

NOTE: All notifications are delivered on the main queue.

//declare this property where it won't go out of scope relative to your listener
let reachability = Reachability()!

//declare this inside of viewWillAppear

     NotificationCenter.default.addObserver(self, selector: #selector(reachabilityChanged(note:)), name: .reachabilityChanged, object: reachability)
    do{
      try reachability.startNotifier()
    }catch{
      print("could not start reachability notifier")
    }

and

@objc func reachabilityChanged(note: Notification) {

  let reachability = note.object as! Reachability

  switch reachability.connection {
  case .wifi:
      print("Reachable via WiFi")
  case .cellular:
      print("Reachable via Cellular")
  case .none:
    print("Network not reachable")
  }
}

and for stopping notifications

reachability.stopNotifier()
NotificationCenter.default.removeObserver(self, name: .reachabilityChanged, object: reachability)

Want to help?

Got a bug fix, or a new feature? Create a pull request and go for it!

Let me know!

If you use Reachability.swift, please let me know about your app and I'll put a link here… and tell your friends!

Cheers, Ash