Skip to main content
Fixed the weird syntax highlighting (as a result, the diff looks more extensive than it really is - use view "Side-by-side Markdown" to compare).
Source Link
Peter Mortensen
  • 31.4k
  • 22
  • 110
  • 134
  1. Install via CocoaPods or Carthage: https://github.com/ashleymills/Reachability.swift

  2. Test reachability via closures

    let reachability = Reachability()!

    reachability.whenReachable = { reachability in if reachability.connection == .wifi { print("Reachable via WiFi") } else { print("Reachable via Cellular") } }

    reachability.whenUnreachable = { _ in print("Not reachable") }

    do { try reachability.startNotifier() } catch { print("Unable to start notifier") }

    let reachability = Reachability()!
    
    reachability.whenReachable = { reachability in
        if reachability.connection == .wifi {
            print("Reachable via WiFi")
        } else {
            print("Reachable via Cellular")
        }
    }
    
    reachability.whenUnreachable = { _ in
        print("Not reachable")
    }
    
    do {
        try reachability.startNotifier()
    } catch {
        print("Unable to start notifier")
    }
    
  1. Add SystemConfiguration framework to the project but don't worry about including it anywhere

  2. Add Tony Million's version of Reachability.h and Reachability.m to the project (found here: https://github.com/tonymillion/Reachability)

  3. Update the interface section

    #import "Reachability.h"

    // Add this to the interface in the .m file of your view controller @interface MyViewController () { Reachability *internetReachableFoo; } @end

    #import "Reachability.h"
    
    // Add this to the interface in the .m file of your view controller
    @interface MyViewController ()
    {
        Reachability *internetReachableFoo;
    }
    @end
    
  4. Then implement this method in the .m file of your view controller which you can call

    // Checks if we have an internet connection or not

    • (void)testInternetConnection {
      internetReachableFoo = [Reachability reachabilityWithHostname:@"www.google.com"];

      // Internet is reachable internetReachableFoo.reachableBlock = ^(Reachability*reach) { // Update the UI on the main thread dispatch_async(dispatch_get_main_queue(), ^{ NSLog(@"Yayyy, we have the interwebs!"); }); };

      // Internet is not reachable internetReachableFoo.unreachableBlock = ^(Reachability*reach) { // Update the UI on the main thread dispatch_async(dispatch_get_main_queue(), ^{ NSLog(@"Someone broke the internet :("); }); };

      [internetReachableFoo startNotifier]; }

    // Checks if we have an internet connection or not
    - (void)testInternetConnection
    {
        internetReachableFoo = [Reachability reachabilityWithHostname:@"www.google.com"];
    
        // Internet is reachable
        internetReachableFoo.reachableBlock = ^(Reachability*reach)
        {
            // Update the UI on the main thread
            dispatch_async(dispatch_get_main_queue(), ^{
                NSLog(@"Yayyy, we have the interwebs!");
            });
        };
    
        // Internet is not reachable
        internetReachableFoo.unreachableBlock = ^(Reachability*reach)
        {
            // Update the UI on the main thread
            dispatch_async(dispatch_get_main_queue(), ^{
                NSLog(@"Someone broke the internet :(");
            });
        };
    
        [internetReachableFoo startNotifier];
    }
    
  1. Install via CocoaPods or Carthage: https://github.com/ashleymills/Reachability.swift

  2. Test reachability via closures

    let reachability = Reachability()!

    reachability.whenReachable = { reachability in if reachability.connection == .wifi { print("Reachable via WiFi") } else { print("Reachable via Cellular") } }

    reachability.whenUnreachable = { _ in print("Not reachable") }

    do { try reachability.startNotifier() } catch { print("Unable to start notifier") }

  1. Add SystemConfiguration framework to the project but don't worry about including it anywhere

  2. Add Tony Million's version of Reachability.h and Reachability.m to the project (found here: https://github.com/tonymillion/Reachability)

  3. Update the interface section

    #import "Reachability.h"

    // Add this to the interface in the .m file of your view controller @interface MyViewController () { Reachability *internetReachableFoo; } @end

  4. Then implement this method in the .m file of your view controller which you can call

    // Checks if we have an internet connection or not

    • (void)testInternetConnection {
      internetReachableFoo = [Reachability reachabilityWithHostname:@"www.google.com"];

      // Internet is reachable internetReachableFoo.reachableBlock = ^(Reachability*reach) { // Update the UI on the main thread dispatch_async(dispatch_get_main_queue(), ^{ NSLog(@"Yayyy, we have the interwebs!"); }); };

      // Internet is not reachable internetReachableFoo.unreachableBlock = ^(Reachability*reach) { // Update the UI on the main thread dispatch_async(dispatch_get_main_queue(), ^{ NSLog(@"Someone broke the internet :("); }); };

      [internetReachableFoo startNotifier]; }

  1. Install via CocoaPods or Carthage: https://github.com/ashleymills/Reachability.swift

  2. Test reachability via closures

    let reachability = Reachability()!
    
    reachability.whenReachable = { reachability in
        if reachability.connection == .wifi {
            print("Reachable via WiFi")
        } else {
            print("Reachable via Cellular")
        }
    }
    
    reachability.whenUnreachable = { _ in
        print("Not reachable")
    }
    
    do {
        try reachability.startNotifier()
    } catch {
        print("Unable to start notifier")
    }
    
  1. Add SystemConfiguration framework to the project but don't worry about including it anywhere

  2. Add Tony Million's version of Reachability.h and Reachability.m to the project (found here: https://github.com/tonymillion/Reachability)

  3. Update the interface section

    #import "Reachability.h"
    
    // Add this to the interface in the .m file of your view controller
    @interface MyViewController ()
    {
        Reachability *internetReachableFoo;
    }
    @end
    
  4. Then implement this method in the .m file of your view controller which you can call

    // Checks if we have an internet connection or not
    - (void)testInternetConnection
    {
        internetReachableFoo = [Reachability reachabilityWithHostname:@"www.google.com"];
    
        // Internet is reachable
        internetReachableFoo.reachableBlock = ^(Reachability*reach)
        {
            // Update the UI on the main thread
            dispatch_async(dispatch_get_main_queue(), ^{
                NSLog(@"Yayyy, we have the interwebs!");
            });
        };
    
        // Internet is not reachable
        internetReachableFoo.unreachableBlock = ^(Reachability*reach)
        {
            // Update the UI on the main thread
            dispatch_async(dispatch_get_main_queue(), ^{
                NSLog(@"Someone broke the internet :(");
            });
        };
    
        [internetReachableFoo startNotifier];
    }
    
Updated for Swift
Source Link
iwasrobbed
  • 46.8k
  • 21
  • 152
  • 195

METHOD 1: Use a simple (ARC and GCD compatible) class to do itImportant: This check should always be performed asynchronously. The majority of answers below are synchronous so be careful otherwise you'll freeze up your app.


Swift

  1. Install via CocoaPods or Carthage: https://github.com/ashleymills/Reachability.swift

  2. Test reachability via closures

    let reachability = Reachability()!

    reachability.whenReachable = { reachability in if reachability.connection == .wifi { print("Reachable via WiFi") } else { print("Reachable via Cellular") } }

    reachability.whenUnreachable = { _ in print("Not reachable") }

    do { try reachability.startNotifier() } catch { print("Unable to start notifier") }


Objective-C

  1. Add SystemConfiguration framework to the project but don't worry about including it anywhere

  2. Add Tony Million's version of Reachability.h and Reachability.m to the project (found here: https://github.com/tonymillion/Reachability)

  3. Update the interface section

    #import "Reachability.h"

    // Add this to the interface in the .m file of your view controller @interface MyViewController () { Reachability *internetReachableFoo; } @end

  4. Then implement this method in the .m file of your view controller which you can call

    // Checks if we have an internet connection or not

    • (void)testInternetConnection {
      internetReachableFoo = [Reachability reachabilityWithHostname:@"www.google.com"];

      // Internet is reachable internetReachableFoo.reachableBlock = ^(Reachability*reach) { // Update the UI on the main thread dispatch_async(dispatch_get_main_queue(), ^{ NSLog(@"Yayyy, we have the interwebs!"); }); };

      // Internet is not reachable internetReachableFoo.unreachableBlock = ^(Reachability*reach) { // Update the UI on the main thread dispatch_async(dispatch_get_main_queue(), ^{ NSLog(@"Someone broke the internet :("); }); };

      [internetReachableFoo startNotifier]; }


 

METHOD 2: Do it yourself the old way using Apple's outdated Reachability class

  1. Add SystemConfiguration framework to the project but don't worry about including it anywhere

  2. Add Apple's version of Reachability.h and Reachability.m to the project (you can get those here)

  3. Add @class Reachability; to the .h file of where you are implementing the code

  4. Create a couple instances to check in the interface section of the .h file:

    Reachability* internetReachable; Reachability* hostReachable;

  5. Add a method in the .h for when the network status updates:

    -(void) checkNetworkStatus:(NSNotification *)notice;

  6. Add #import "Reachability.h" to the .m file where you are implementing the check

  7. In the .m file of where you are implementing the check, you can place this in one of the first methods called (init or viewWillAppear or viewDidLoad etc):

    -(void) viewWillAppear:(BOOL)animated { // check for internet connection [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkNetworkStatus:) name:kReachabilityChangedNotification object:nil];

     internetReachable = [Reachability reachabilityForInternetConnection];
     [internetReachable startNotifier];
    
     // check if a pathway to a random host exists
     hostReachable = [Reachability reachabilityWithHostName:@"www.apple.com"];
     [hostReachable startNotifier];
    
     // now patiently wait for the notification
    

    }

  8. Set up the method for when the notification gets sent and set whatever checks or call whatever methods you may have set up (in my case, I just set a BOOL)

    -(void) checkNetworkStatus:(NSNotification *)notice { // called after network status changes NetworkStatus internetStatus = [internetReachable currentReachabilityStatus]; switch (internetStatus) { case NotReachable: { NSLog(@"The internet is down."); self.internetActive = NO;

             break;
         }
         case ReachableViaWiFi:
         {
             NSLog(@"The internet is working via WIFI.");
             self.internetActive = YES;
    
             break;
         }
         case ReachableViaWWAN:
         {
             NSLog(@"The internet is working via WWAN.");
             self.internetActive = YES;
    
             break;
         }
     }
    
     NetworkStatus hostStatus = [hostReachable currentReachabilityStatus];
     switch (hostStatus)
     {
         case NotReachable:
         {
             NSLog(@"A gateway to the host server is down.");
             self.hostActive = NO;
    
             break;
         }
         case ReachableViaWiFi:
         {
             NSLog(@"A gateway to the host server is working via WIFI.");
             self.hostActive = YES;
    
             break;
         }
         case ReachableViaWWAN:
         {
             NSLog(@"A gateway to the host server is working via WWAN.");
             self.hostActive = YES;
    
             break;
         }
     }
    

    }

  9. In your dealloc or viewWillDisappear or similar method, remove yourself as an observer

    -(void) viewWillDisappear:(BOOL)animated { [[NSNotificationCenter defaultCenter] removeObserver:self]; }

Note: There might be an instance using viewWillDisappear where you receive a memory warning and the observer never gets unregistered so you should account for that as well.


Note: The domain you use doesn't matter. It's just testing for a gateway to any domain.

Important Note: The Reachability class is one of the most used classes in projects so you might run into naming conflicts with other projects like ShareKit. If this happens, you'll have to rename one of the pairs of Reachability.h and Reachability.m files to something else to resolve the issue.

Note: The domain you use doesn't matter. It's just testing for a gateway to any domain.

METHOD 1: Use a simple (ARC and GCD compatible) class to do it

  1. Add SystemConfiguration framework to the project but don't worry about including it anywhere

  2. Add Tony Million's version of Reachability.h and Reachability.m to the project (found here: https://github.com/tonymillion/Reachability)

  3. Update the interface section

    #import "Reachability.h"

    // Add this to the interface in the .m file of your view controller @interface MyViewController () { Reachability *internetReachableFoo; } @end

  4. Then implement this method in the .m file of your view controller which you can call

    // Checks if we have an internet connection or not

    • (void)testInternetConnection {
      internetReachableFoo = [Reachability reachabilityWithHostname:@"www.google.com"];

      // Internet is reachable internetReachableFoo.reachableBlock = ^(Reachability*reach) { // Update the UI on the main thread dispatch_async(dispatch_get_main_queue(), ^{ NSLog(@"Yayyy, we have the interwebs!"); }); };

      // Internet is not reachable internetReachableFoo.unreachableBlock = ^(Reachability*reach) { // Update the UI on the main thread dispatch_async(dispatch_get_main_queue(), ^{ NSLog(@"Someone broke the internet :("); }); };

      [internetReachableFoo startNotifier]; }


 

METHOD 2: Do it yourself the old way using Apple's outdated Reachability class

  1. Add SystemConfiguration framework to the project but don't worry about including it anywhere

  2. Add Apple's version of Reachability.h and Reachability.m to the project (you can get those here)

  3. Add @class Reachability; to the .h file of where you are implementing the code

  4. Create a couple instances to check in the interface section of the .h file:

    Reachability* internetReachable; Reachability* hostReachable;

  5. Add a method in the .h for when the network status updates:

    -(void) checkNetworkStatus:(NSNotification *)notice;

  6. Add #import "Reachability.h" to the .m file where you are implementing the check

  7. In the .m file of where you are implementing the check, you can place this in one of the first methods called (init or viewWillAppear or viewDidLoad etc):

    -(void) viewWillAppear:(BOOL)animated { // check for internet connection [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkNetworkStatus:) name:kReachabilityChangedNotification object:nil];

     internetReachable = [Reachability reachabilityForInternetConnection];
     [internetReachable startNotifier];
    
     // check if a pathway to a random host exists
     hostReachable = [Reachability reachabilityWithHostName:@"www.apple.com"];
     [hostReachable startNotifier];
    
     // now patiently wait for the notification
    

    }

  8. Set up the method for when the notification gets sent and set whatever checks or call whatever methods you may have set up (in my case, I just set a BOOL)

    -(void) checkNetworkStatus:(NSNotification *)notice { // called after network status changes NetworkStatus internetStatus = [internetReachable currentReachabilityStatus]; switch (internetStatus) { case NotReachable: { NSLog(@"The internet is down."); self.internetActive = NO;

             break;
         }
         case ReachableViaWiFi:
         {
             NSLog(@"The internet is working via WIFI.");
             self.internetActive = YES;
    
             break;
         }
         case ReachableViaWWAN:
         {
             NSLog(@"The internet is working via WWAN.");
             self.internetActive = YES;
    
             break;
         }
     }
    
     NetworkStatus hostStatus = [hostReachable currentReachabilityStatus];
     switch (hostStatus)
     {
         case NotReachable:
         {
             NSLog(@"A gateway to the host server is down.");
             self.hostActive = NO;
    
             break;
         }
         case ReachableViaWiFi:
         {
             NSLog(@"A gateway to the host server is working via WIFI.");
             self.hostActive = YES;
    
             break;
         }
         case ReachableViaWWAN:
         {
             NSLog(@"A gateway to the host server is working via WWAN.");
             self.hostActive = YES;
    
             break;
         }
     }
    

    }

  9. In your dealloc or viewWillDisappear or similar method, remove yourself as an observer

    -(void) viewWillDisappear:(BOOL)animated { [[NSNotificationCenter defaultCenter] removeObserver:self]; }

Note: There might be an instance using viewWillDisappear where you receive a memory warning and the observer never gets unregistered so you should account for that as well.


Note: The domain you use doesn't matter. It's just testing for a gateway to any domain.

Important Note: The Reachability class is one of the most used classes in projects so you might run into naming conflicts with other projects like ShareKit. If this happens, you'll have to rename one of the pairs of Reachability.h and Reachability.m files to something else to resolve the issue.

Important: This check should always be performed asynchronously. The majority of answers below are synchronous so be careful otherwise you'll freeze up your app.


Swift

  1. Install via CocoaPods or Carthage: https://github.com/ashleymills/Reachability.swift

  2. Test reachability via closures

    let reachability = Reachability()!

    reachability.whenReachable = { reachability in if reachability.connection == .wifi { print("Reachable via WiFi") } else { print("Reachable via Cellular") } }

    reachability.whenUnreachable = { _ in print("Not reachable") }

    do { try reachability.startNotifier() } catch { print("Unable to start notifier") }


Objective-C

  1. Add SystemConfiguration framework to the project but don't worry about including it anywhere

  2. Add Tony Million's version of Reachability.h and Reachability.m to the project (found here: https://github.com/tonymillion/Reachability)

  3. Update the interface section

    #import "Reachability.h"

    // Add this to the interface in the .m file of your view controller @interface MyViewController () { Reachability *internetReachableFoo; } @end

  4. Then implement this method in the .m file of your view controller which you can call

    // Checks if we have an internet connection or not

    • (void)testInternetConnection {
      internetReachableFoo = [Reachability reachabilityWithHostname:@"www.google.com"];

      // Internet is reachable internetReachableFoo.reachableBlock = ^(Reachability*reach) { // Update the UI on the main thread dispatch_async(dispatch_get_main_queue(), ^{ NSLog(@"Yayyy, we have the interwebs!"); }); };

      // Internet is not reachable internetReachableFoo.unreachableBlock = ^(Reachability*reach) { // Update the UI on the main thread dispatch_async(dispatch_get_main_queue(), ^{ NSLog(@"Someone broke the internet :("); }); };

      [internetReachableFoo startNotifier]; }

Important Note: The Reachability class is one of the most used classes in projects so you might run into naming conflicts with other projects. If this happens, you'll have to rename one of the pairs of Reachability.h and Reachability.m files to something else to resolve the issue.

Note: The domain you use doesn't matter. It's just testing for a gateway to any domain.

ARC
Source Link
iwasrobbed
  • 46.8k
  • 21
  • 152
  • 195
  1. Add SystemConfiguration framework to the project but don't worry about including it anywhere

  2. Add Apple's version of Reachability.h and Reachability.m to the project (you can get those here)

  3. Add @class Reachability; to the .h file of where you are implementing the code

  4. Create a couple instances to check in the interface section of the .h file:

    Reachability* internetReachable; Reachability* hostReachable;

  5. Add a method in the .h for when the network status updates:

    -(void) checkNetworkStatus:(NSNotification *)notice;

  6. Add #import "Reachability.h" to the .m file where you are implementing the check

  7. In the .m file of where you are implementing the check, you can place this in one of the first methods called (init or viewWillAppear or viewDidLoad etc):

    -(void) viewWillAppear:(BOOL)animated { // check for internet connection [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkNetworkStatus:) name:kReachabilityChangedNotification object:nil];

     internetReachable = [[Reachability reachabilityForInternetConnection][Reachability retain];reachabilityForInternetConnection];
     [internetReachable startNotifier];
    
     // check if a pathway to a random host exists
     hostReachable = [[Reachability[Reachability reachabilityWithHostName: @"www.apple.com"] retain];com"];
     [hostReachable startNotifier];
    
     // now patiently wait for the notification
    

    }

  8. Set up the method for when the notification gets sent and set whatever checks or call whatever methods you may have set up (in my case, I just set a BOOL)

    -(void) checkNetworkStatus:(NSNotification *)notice { // called after network status changes NetworkStatus internetStatus = [internetReachable currentReachabilityStatus]; switch (internetStatus) { case NotReachable: { NSLog(@"The internet is down."); self.internetActive = NO;

             break;
         }
         case ReachableViaWiFi:
         {
             NSLog(@"The internet is working via WIFI.");
             self.internetActive = YES;
    
             break;
         }
         case ReachableViaWWAN:
         {
             NSLog(@"The internet is working via WWAN.");
             self.internetActive = YES;
    
             break;
         }
     }
    
     NetworkStatus hostStatus = [hostReachable currentReachabilityStatus];
     switch (hostStatus)
     {
         case NotReachable:
         {
             NSLog(@"A gateway to the host server is down.");
             self.hostActive = NO;
    
             break;
         }
         case ReachableViaWiFi:
         {
             NSLog(@"A gateway to the host server is working via WIFI.");
             self.hostActive = YES;
    
             break;
         }
         case ReachableViaWWAN:
         {
             NSLog(@"A gateway to the host server is working via WWAN.");
             self.hostActive = YES;
    
             break;
         }
     }
    

    }

  9. In your dealloc or viewWillDisappear or similar method, remove yourself as an observer

    -(void) viewWillDisappear:(BOOL)animated { [[NSNotificationCenter defaultCenter] removeObserver:self]; }

  1. Add SystemConfiguration framework to the project but don't worry about including it anywhere

  2. Add Apple's version of Reachability.h and Reachability.m to the project (you can get those here)

  3. Add @class Reachability; to the .h file of where you are implementing the code

  4. Create a couple instances to check in the interface section of the .h file:

    Reachability* internetReachable; Reachability* hostReachable;

  5. Add a method in the .h for when the network status updates:

    -(void) checkNetworkStatus:(NSNotification *)notice;

  6. Add #import "Reachability.h" to the .m file where you are implementing the check

  7. In the .m file of where you are implementing the check, you can place this in one of the first methods called (init or viewWillAppear or viewDidLoad etc):

    -(void) viewWillAppear:(BOOL)animated { // check for internet connection [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkNetworkStatus:) name:kReachabilityChangedNotification object:nil];

     internetReachable = [[Reachability reachabilityForInternetConnection] retain];
     [internetReachable startNotifier];
    
     // check if a pathway to a random host exists
     hostReachable = [[Reachability reachabilityWithHostName: @"www.apple.com"] retain];
     [hostReachable startNotifier];
    
     // now patiently wait for the notification
    

    }

  8. Set up the method for when the notification gets sent and set whatever checks or call whatever methods you may have set up (in my case, I just set a BOOL)

    -(void) checkNetworkStatus:(NSNotification *)notice { // called after network status changes NetworkStatus internetStatus = [internetReachable currentReachabilityStatus]; switch (internetStatus) { case NotReachable: { NSLog(@"The internet is down."); self.internetActive = NO;

             break;
         }
         case ReachableViaWiFi:
         {
             NSLog(@"The internet is working via WIFI.");
             self.internetActive = YES;
    
             break;
         }
         case ReachableViaWWAN:
         {
             NSLog(@"The internet is working via WWAN.");
             self.internetActive = YES;
    
             break;
         }
     }
    
     NetworkStatus hostStatus = [hostReachable currentReachabilityStatus];
     switch (hostStatus)
     {
         case NotReachable:
         {
             NSLog(@"A gateway to the host server is down.");
             self.hostActive = NO;
    
             break;
         }
         case ReachableViaWiFi:
         {
             NSLog(@"A gateway to the host server is working via WIFI.");
             self.hostActive = YES;
    
             break;
         }
         case ReachableViaWWAN:
         {
             NSLog(@"A gateway to the host server is working via WWAN.");
             self.hostActive = YES;
    
             break;
         }
     }
    

    }

  9. In your dealloc or viewWillDisappear or similar method, remove yourself as an observer

    -(void) viewWillDisappear:(BOOL)animated { [[NSNotificationCenter defaultCenter] removeObserver:self]; }

  1. Add SystemConfiguration framework to the project but don't worry about including it anywhere

  2. Add Apple's version of Reachability.h and Reachability.m to the project (you can get those here)

  3. Add @class Reachability; to the .h file of where you are implementing the code

  4. Create a couple instances to check in the interface section of the .h file:

    Reachability* internetReachable; Reachability* hostReachable;

  5. Add a method in the .h for when the network status updates:

    -(void) checkNetworkStatus:(NSNotification *)notice;

  6. Add #import "Reachability.h" to the .m file where you are implementing the check

  7. In the .m file of where you are implementing the check, you can place this in one of the first methods called (init or viewWillAppear or viewDidLoad etc):

    -(void) viewWillAppear:(BOOL)animated { // check for internet connection [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkNetworkStatus:) name:kReachabilityChangedNotification object:nil];

     internetReachable = [Reachability reachabilityForInternetConnection];
     [internetReachable startNotifier];
    
     // check if a pathway to a random host exists
     hostReachable = [Reachability reachabilityWithHostName:@"www.apple.com"];
     [hostReachable startNotifier];
    
     // now patiently wait for the notification
    

    }

  8. Set up the method for when the notification gets sent and set whatever checks or call whatever methods you may have set up (in my case, I just set a BOOL)

    -(void) checkNetworkStatus:(NSNotification *)notice { // called after network status changes NetworkStatus internetStatus = [internetReachable currentReachabilityStatus]; switch (internetStatus) { case NotReachable: { NSLog(@"The internet is down."); self.internetActive = NO;

             break;
         }
         case ReachableViaWiFi:
         {
             NSLog(@"The internet is working via WIFI.");
             self.internetActive = YES;
    
             break;
         }
         case ReachableViaWWAN:
         {
             NSLog(@"The internet is working via WWAN.");
             self.internetActive = YES;
    
             break;
         }
     }
    
     NetworkStatus hostStatus = [hostReachable currentReachabilityStatus];
     switch (hostStatus)
     {
         case NotReachable:
         {
             NSLog(@"A gateway to the host server is down.");
             self.hostActive = NO;
    
             break;
         }
         case ReachableViaWiFi:
         {
             NSLog(@"A gateway to the host server is working via WIFI.");
             self.hostActive = YES;
    
             break;
         }
         case ReachableViaWWAN:
         {
             NSLog(@"A gateway to the host server is working via WWAN.");
             self.hostActive = YES;
    
             break;
         }
     }
    

    }

  9. In your dealloc or viewWillDisappear or similar method, remove yourself as an observer

    -(void) viewWillDisappear:(BOOL)animated { [[NSNotificationCenter defaultCenter] removeObserver:self]; }

added 101 characters in body
Source Link
iwasrobbed
  • 46.8k
  • 21
  • 152
  • 195
Loading
added 95 characters in body
Source Link
iwasrobbed
  • 46.8k
  • 21
  • 152
  • 195
Loading
Updated with an example for Tony Million's library
Source Link
iwasrobbed
  • 46.8k
  • 21
  • 152
  • 195
Loading
Updated with an example for Tony Million's library
Source Link
iwasrobbed
  • 46.8k
  • 21
  • 152
  • 195
Loading
added 130 characters in body
Source Link
iwasrobbed
  • 46.8k
  • 21
  • 152
  • 195
Loading
improved code formatting.
Source Link
Richard J. Ross III
  • 55.7k
  • 25
  • 139
  • 205
Loading
added 120 characters in body
Source Link
iwasrobbed
  • 46.8k
  • 21
  • 152
  • 195
Loading
Source Link
iwasrobbed
  • 46.8k
  • 21
  • 152
  • 195
Loading