2

I am working on an iOS app that requires an internet connection. I've noticed that the FaceBook app updates the screen to tell you when you have lost a connection. It also removes the warning when you regain your connection.

My question is how does this happen? Since the view is already loaded there must be some type of notification that is called if a connection is lost or recovered. Tonight I installed the popular Reachability code from GitHub and if I disconnect my connection once the view is loaded I am not notified that the connection was lost. I did sign up for a notification. The code is below.

override func viewWillAppear(animated: Bool)
    {
        super.viewWillAppear(animated)

        var reachability: Reachability?

        //declare this inside of viewWillAppear
        do {
            reachability = try Reachability.reachabilityForInternetConnection()
        } catch {
            print("Unable to create Reachability")
            return
        }

        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(TipViewController.reachabilityChanged(_:)),name: ReachabilityChangedNotification,object: reachability)
        do{
            try reachability?.startNotifier()
        }catch{
            print("could not start reachability notifier")
        }
    }

    func reachabilityChanged(note: NSNotification) {

        let reachability = note.object as! Reachability

        if reachability.isReachable() {
            if reachability.isReachableViaWiFi() {
                print("Reachable via WiFi")
            } else {
                print("Reachable via Cellular")
            }
        } else {
            print("Network not reachable")
        }
    }

Any help would be appreciated.

jonthornham
  • 2,881
  • 4
  • 19
  • 35
  • Any solution? Is the method getting triggered when there is a change in internet connection? – Ashok Jul 04 '17 at 08:40
  • I was experimenting and solved it. If I declare the reachability instance globally, It worked. Thanks. – Ashok Jul 04 '17 at 08:48

2 Answers2

0

First add Reachability.swift file into your project that you can get from here below link

https://github.com/pavangandhi/TestProject/tree/master/TestProject/AppConstants

Use the below given code to test app is connected with internet or not :

// check internet connectivity if its returns YES means its connect with internet, Wifi and cellular data

 class func isConnectedToNetwork() -> Bool 
 {
    let reachability = Reachability.reachabilityForInternetConnection()

    if reachability.isReachable() || reachability.isReachableViaWiFi() || reachability.isReachableViaWWAN()
    {
        return true
    }
    else
    {
        return false
    }
}

Note : You need to add Reachability.swift file into your project before adding this code.

Lotus Shah
  • 493
  • 4
  • 13
  • 1
    I don't see how this updates me if I turn wifi off or on without reloading the view. Am I missing something? – jonthornham Jun 23 '16 at 16:25
  • whenever u go for performing any events which required net connection so on that time you can used this and to show pop up to user. – Lotus Shah Jun 24 '16 at 03:56
  • 1
    I know that this is an option but this doesn't not solve the question I asked. Thanks for the help regardless. – jonthornham Jun 25 '16 at 18:03
0

Use This

And then call isConnectedToNetwork() where u want like:

guard isConnectedToNetwork() != false else{
//Do your thing
return
}
// Do your thing if is connected is true
Community
  • 1
  • 1
Mtoklitz113
  • 3,828
  • 3
  • 21
  • 40