1

I am using ashleymills/Reachability.swift to check network connection status. When I use it in a particular class in viewWillAppear method, it works just fine. But currently I have more that 40 classes and I don't think I need to implement the notification listener in each of them. So, I have been trying to run it in AppDelegate's application didFinishLaunchingWithOptions method. I tried for one class, like, if I have a class named DashboardVC, I wrote:

var reachability = Reachability()!
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let vc = DashboardVC()
Notificationcenter.default.addObserver(vc, selector: #selector(reachabilityDidChange(notification:)), name: .reachabilityChanged, object: nil)
return true
}

And the reachabilityDidChange(notification:) function:

func reachabilityDidChange(notification: Notification) {

let reachability1 = notification.object as! Reachability  
switch reachability1.connection {  
case .wifi:      
   print("Reachable via WiFi")  
case .cellular:      
   print("Reachable via Cellular")  
case .none:    
   print("Network not reachable")  }
}

But this didn't work when I turned on/off the internet connection or switched from wifi to cellular network right when DashboardVC was the topmostViewController. Please help me:

  1. If there's a way to achieve my goal with ashleymills/Reachability.swift
  2. If there's a way to achieve this without ashleymills/Reachability.swift
  3. If there's any other way to keep track of network connection status

Regards!

Ashley Mills
  • 41,127
  • 14
  • 115
  • 144

0 Answers0