0

I used Reachability Class from apple to check internet connection. I'll share to codes in the below. It's working fine if i open the app with online. But if i try to open app offline Reachability function is not triggered. Can anyone help me to solve this issue. Thank you

Here is the code which are locate in AppDelegate.swift

    var reachability: Reachability?

        var window: UIWindow?

        func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {


            NSNotificationCenter.defaultCenter().addObserver(self, selector: "checkForReachability:", name:kReachabilityChangedNotification, object: nil)

            reachability = Reachability.reachabilityForInternetConnection();

            reachability?.startNotifier();


            return true
        }


func checkForReachability(notification: NSNotification) {

        let title_alert = NSLocalizedString("Connection Problem", comment: "Connection Problem")
        let remoteHostStatus = self.reachability!.currentReachabilityStatus()
        if (remoteHostStatus == NotReachable) {
            let myAlert = UIAlertController(title:title_alert, message:NSLocalizedString("Please Check Your Connection", comment: "Connection Problem"),  preferredStyle: UIAlertControllerStyle.Alert)
            let okAction = UIAlertAction(title:NSLocalizedString("Try Again",comment:"Connection Problem"), style:UIAlertActionStyle.Default) {
                action in
                self.checkForReachability(notification)
            }
            myAlert.addAction(okAction)
            self.window?.rootViewController?.presentViewController(myAlert, animated:true, completion:nil)
        }
    }
Esat kemal Ekren
  • 516
  • 2
  • 8
  • 28
  • You'll want to register a callback function which is called if the connection availability changes. See http://stackoverflow.com/questions/33551191/swift-pass-data-to-a-closure-that-captures-context for an example. – Martin R Apr 06 '16 at 11:17

3 Answers3

1

Reachability send you notifications when the state changes. If you have no internet, and this doesn't change, no notification will ever be sent. You need to check once by hand, probably after creating the reachability object. To check, you just check currentReachabilityStatus() outside the notification callback.

PS. I wouldn't send an alert when the internet goes away, but when the user actually does something that would require the internet and it doesn't work. Things like background updates shouldn't be visible to the user and shouldn't show an alert. (Bad example is iTunes on a Mac, which tells you all the time that it couldn't connect to the iTunes store, when I had no intention whatsoever to connect to it).

What exactly you do obviously depends on your app. Take Safari - it doesn't tell you there is no internet connection, only when you connect to a page. There are plenty of features that are useful without internet access.

If your app is absolutely useless without internet access and there is nothing better you can do, display a page that says "no internet". If there are things the user can do, let them do those things. Programmers often see these things from a programmer's point of view: Something went wrong, and everyone told you that you should inform the user. From the user's perspective: Being told with repeated alerts will get on their nerves. Imagine you are a user who just turned on AirPlane mode. That user thinks "for heaven's sake, I know there is no WiFi because I just turned it off. Don't keep telling me".

gnasher729
  • 51,477
  • 5
  • 75
  • 98
0

This is a hard one to answer with all of the code that Reachability requires, but here is the video I used when I set up Reachability for my app! It uses NSNotificationCenter to constantly check if the user is online. When the user goes offline, you can have an action occur until the user comes back online.

Dan Levy
  • 3,931
  • 4
  • 28
  • 48
  • First of all thank you for answering. But in my app if the user was online and launch the app everythings works perfectly while they use if the got offline notification appear immediately. I figured it out until here. But i don't want to user open my app if they try to reach offline. That's the problem that i want to solve – Esat kemal Ekren Apr 06 '16 at 12:01
  • Ok I see...so you want the app not to open of the user is offline? – Dan Levy Apr 06 '16 at 12:23
  • Gotcha...unfortunately, you can't do that. Apple will reject the app if they see that the app can't open. – Dan Levy Apr 06 '16 at 12:25
  • then say not open, when i execute this code pop up notification appears when users gone offline. How can i opened my app and show the users you 're not connect to internet pages like facebooks – Esat kemal Ekren Apr 06 '16 at 12:27
  • I would take a look at that video I posted in my answer. It shows how to do that I believe. – Dan Levy Apr 06 '16 at 12:28
  • I'll add video of my app how is act like. i hope i can show you to exactly what i want – Esat kemal Ekren Apr 06 '16 at 12:29
0

create a UIViewController extension and add a function like this -

func networkChanged() {
    if let isReachable = Reachability()?.isReachable, isReachable == true {
        noInternetErrorManager.removeNoInternetView()
    } else {
        noInternetErrorManager.displayNoInternetError(forParentView: view)
    }
}

Now call this function on viewDidLoad()

Dhiraj Das
  • 174
  • 9