Reachability.swift: Notification is only fired once.

Created on 20 Aug 2017  ·  3Comments  ·  Source: ashleymills/Reachability.swift

import UIKit
import ReachabilitySwift

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, GIDSignInDelegate {

var window: UIWindow?
var reachability: Reachability!

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    window = UIWindow()
    reachability = Reachability.init()

    NotificationCenter.default.addObserver(self, selector: #selector(reachabilityChanged),name: ReachabilityChangedNotification, object: nil)

    do {
        try reachability!.startNotifier()
    } catch {
        print("could not start reachability notifier")
    }

    return true
}

func reachabilityChanged() {
    DispatchQueue.main.async {
        //self.networkStatusChanged()
        print(self.reachability.currentReachabilityStatus)
    }
}

}

Most helpful comment

I had same issue. but fixed it.
It was because I created reachability object inside wiewDidLoad function. I fixed it and moving declaration in class.

In Reachability class they called stopNotifire on deinit function.

All 3 comments

I had same issue. but fixed it.
It was because I created reachability object inside wiewDidLoad function. I fixed it and moving declaration in class.

In Reachability class they called stopNotifire on deinit function.

Bro, I've found that there was something wrong with my mackbook pro. Don't know why but in my macbook simulator this code wont work but when I ran the app in a real device the code worked like a charm.

It was because I created reachability object inside wiewDidLoad function. I fixed it and moving declaration in class

@ashleymills Could you please add this info to README ?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

AlekseiR picture AlekseiR  ·  4Comments

sreejithsn picture sreejithsn  ·  5Comments

jet-snag picture jet-snag  ·  3Comments

tjarbo picture tjarbo  ·  12Comments

bclymer picture bclymer  ·  12Comments