Reachability.swift icon indicating copy to clipboard operation
Reachability.swift copied to clipboard

Can only get the Notification to notice 1 change then it stops noticing them.

Open bostonareahuman opened this issue 9 years ago • 15 comments

So I have an app I'm developing. I'd like to thank Mr Mills for his contribution with this library.

In my AppDelegate:

var reachability: Reachability!


@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

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


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

        reachability = Reachability.init()
        do {
            try reachability.startNotifier()
            print("setting up notifier")
        } catch {
            print("notifier failed.")
        }
            return true
    }

Then I have a View Controller where I set up the reference to the notifier and try to listen for the changes to it. It will change the label when I disconnect the WiFi however when it reconnectes there is no change to the label and the notifier doesn't fire off the function.

class contactPrefsViewController: UIViewController {

    @IBOutlet weak var labelConnected: UILabel!
    var reachability2: Reachability!


    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func viewDidAppear(_ animated: Bool) {

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

    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func reachabilityChanged(notify:Notification){
        let reachability2 = notify.object as! Reachability
        print("doing notification");
        if reachability2.isReachable{
            self.labelConnected.text = "connected"
            print("connected")
        }else{
            self.labelConnected.text = "not connected"
            print("not Connected")
        }
    }

Any advice appreciated.

bostonareahuman avatar Oct 16 '16 19:10 bostonareahuman

iOS 10 or less?

bellots avatar Oct 16 '16 19:10 bellots

Simulator is running in iOS10...xCode 8 . Cocoapods with a target of 9.0

bostonareahuman avatar Oct 16 '16 20:10 bostonareahuman

We noticed that there's a problem with iOS 10 and this framework. Unfortunately we haven't found the solution yet. Can you investigate too? follow Issue #151 and stay there in touch.

bellots avatar Oct 16 '16 20:10 bellots

I'm a new swift developer(2 months) but I moved the startNotifier into the controller itself. It would appear it has to be a scope issue where the scope to the notifier started in delegate is destroyed upon first use.

Still not working as it should. It isn't that is isn't calling the notifier just when it is turned back on. It isn't calling the notifier at all after the inital call to it.

I noticed that the Notifier is getting called multple times when I loop back to a controller from another one...once...then twice....just overall very strange behavior. I'm going to remove it from my code until there is a remedy and just check for the reachability if I need to pull data.

bostonareahuman avatar Oct 16 '16 21:10 bostonareahuman

I'm running in the simulator with iOS8.3 and Xcode 8, and seeing the same symptom. I get one reachability notification after startup, but no further notifications when going on or offline.

The same code worked prior to an update to Xcode 8 (i.e. under Xcode 7).

My codebase is Objective C, not Swift.

davidralley avatar Nov 30 '16 18:11 davidralley

I am seeing the same behaviour, did anyone find a fix for this? Xcode 8 as well though this is a new project so I can not say if it was working in 7. Running on device through Xcode. iOS 9.3.2 is on device. Instead of NSNotifications though our set up is with closures.

abbeyjackson avatar Jan 19 '17 05:01 abbeyjackson

run into the same problem running it on an iPad in iOS10

archywillhe avatar Mar 29 '17 16:03 archywillhe

Still not fixed? :(

kmalkic avatar May 09 '17 00:05 kmalkic

encounter the same issues too

kohdesmond avatar May 11 '17 09:05 kohdesmond

I made a hacky fix for it. basically when it become unreachable, the self.reachabilityRef breaks internally, so what i did is creating a timer that recreate a reachabilityRef every second until it get back online. It worked for me.

kmalkic avatar May 11 '17 09:05 kmalkic

i'm currently using Alamofire's NetworkReachabilityManager

func checkNetworkReachability() {
        let mgr  = NetworkReachabilityManager()
        mgr?.listener = { status in
            print("Network Status Changed: \(status)")
            kNetworkNotConnected = mgr?.isReachable == true ? false : true
        }
        mgr?.startListening()
    }

kohdesmond avatar May 11 '17 09:05 kohdesmond

Would be awesome if this was eventually fixed...how does anyone use this?

bostonareahuman avatar Jan 28 '18 14:01 bostonareahuman

It's not working for me either.

Environment - iPhone 8+ Simulator iOS 11.2 (Xcode 9.2).

hemangshah avatar Apr 15 '18 07:04 hemangshah

Not working here either..

wdcurry avatar May 16 '18 19:05 wdcurry

@kohdesmond : great tip, thank you! Copied the one .swift file from https://github.com/Alamofire/Alamofire/blob/master/Source/NetworkReachabilityManager.swift, dropped the mgr to ViewController scope, added code similar to your's, and on every viewDidLoad, i have initial reachability and virtually instant change notifs.. THANK YOU.

My apologies to this repo for the plug elsewhere, but it should be this simple. Cheers!

wdcurry avatar May 16 '18 20:05 wdcurry