My Reachability Notifier is only able to be called once

二次信任 提交于 2019-12-04 02:16:06

问题


So, I have the following in my AppDelegate.
It will notify my when I turn my WIFI off but will not react after that initial run.
I have had this working in the past.
I'm on swift 3 with Xcode 8 and the reachability that is for this version of the swift and xCode.

I'm hoping to get a solution to this.

Thanks.

    var reachability: Reachability?

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

        self.reachability = Reachability()

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

        do {
            try self.reachability?.startNotifier()
        } catch {
            print("Unable to start Notifier")
        }

        return true
    }

    func reachabilityChanged(_ note:Notification){
        print("is in here")
        let reachability = note.object as! Reachability
        if reachability.isReachable{
            print("is Reachable")
            // self.amConnected.text = "YES"
            //self.amConnected.fadeOut(duration: 2.0)
        }else{
            print("IsNotReachable")
            //self.amConnected.text = "No"
            //self.amConnected.fadeIn(duration: 2.0)
        }
        print("Changed status")    
    }

回答1:


There are some changes in Reachability in Swift 3. Download the latest Reachability.swift file and add that in your project. Link

For Swift 2.x code please check my answer here

Swift 3.x code

Now in your AppDelegate take a Reachability class object

private var reachability:Reachability!

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

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

    self.reachability = Reachability.init()
    do {
        try self.reachability.startNotifier()
    } catch {
    }
    return true
}

reachabilityChanged method defination

//MARK:- Network Check
func reachabilityChanged(notification:Notification) {
    let reachability = notification.object as! Reachability
    if reachability.isReachable {
        if reachability.isReachableViaWiFi {
            print("Reachable via WiFi")
        } else {
            print("Reachable via Cellular")
        }
    } else {
         print("Network not reachable")
    }
}

This will be always called whenever user switches from Wifi to Cellular and vice versa or when Network Connects and Disconnects and vice versa.
Working fine in my case.

Read the documentation for more details in Swift 3 breaking changes section

Made a sample for you

https://www.dropbox.com/sh/bph33b12tyc7fpd/AAD2pGbgW3UnqgQoe7MGPpKPa?dl=0




回答2:


Try passing nil for the object parameter in addObserver. Also, for Swift 3, I had to initialize it with self.reachability = Reachability.forInternetConnection()



来源:https://stackoverflow.com/questions/40066530/my-reachability-notifier-is-only-able-to-be-called-once

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!