Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3802336
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T14:11:34+00:00 2026-05-19T14:11:34+00:00

Ok so I am trying to get this app to show network error alert

  • 0

Ok so I am trying to get this app to show network error alert codes. I have added the SystemConfiguration.framework framework and Apple’s "Reachability" sample code.

Here is the viewcontroller.h file:

#import <UIKit/UIKit.h>

@class Reachability;

@interface Test_Internet_ConnectionViewController : UIViewController {

    Reachability* internetReachable;
    Reachability* hostReachable;
}

@property BOOL internetActive;
@property BOOL hostActive;

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

@end

Here is the viewcontroller.m file:

#import "Test_Internet_ConnectionViewController.h"
#import "Reachability.h";

@implementation Test_Internet_ConnectionViewController

@synthesize internetActive;
@synthesize hostActive;

/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];

        // 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
    }

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (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;

            UIAlertView *errorView = [[UIAlertView alloc] initWithTitle:@"Internet Unavailable" message:@"This page cannot load, please check your internet connection and try again." delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
            [errorView show];
            [errorView release];

            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;

        }
    }
}

- (void)dealloc {
    [super dealloc];

        [[NSNotificationCenter defaultCenter] removeObserver:self];

    }

@end

If there is no internet upon opening the app the error works as intended, but if the app loses connection after the it has initially opened with an internet connection the error displays 3 times, when I obviously only want it to display once.

I think it may be something in the Reachability class where it sends the notification 3 times, maybe in 3 different places. I don’t know why it does this, but I think the solution may be to remove the two calls I don’t want from the Reachability class but I don’t know how.

Can anyone help me, please?
Thanks in advance.

EDIT:

The 8 warnings and 4 errors are as follows:

‘TestViewController’ may not respond to ‘+checkConnectivity’

Initialization makes integer from pointer without a cast

Local declaration of ‘connectivity’ hides instance variable

‘MyReachability’ undeclared

‘reachability’ undeclared

‘TestViewController’ may not respond to ‘-siteAvailable’

‘TestViewController’ may not respond to ‘-addConnectivityView’

Redefinition of ‘-[TestViewController reachabilityChanged:]’

‘MyReachability’ undeclared

TestViewController may not respond to ‘-siteAvailable’

TestViewController may not respond to ‘-addConnectivityView’

TestViewController may not respond to ‘-removeConnectivityView’

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T14:11:35+00:00Added an answer on May 19, 2026 at 2:11 pm

    Yes, it sends notifications multiple times for some reason. To overcome this I maintained a network status variable in application delegate, which was an observer for kReachabilityChangedNotification. The view was an observer for a custom kViewReachabilityChangedNotification, which was sent by an application delegate when the value actually changed. More than that, after getting a notification from reachability I called a Reachability function on getting a current network status and changed my application delegate variable accordingly. Then I could send a notification to a view to update itself.

    This workaround is ugly, but works. I spent plenty of time on understanding why notifications being sent this way and finally gave up finding this solution.

    EDIT:
    Here’s your sample – I still don’t have a good place to put all working examples, so please try to figure out the solution from the code sample:
    Subclass Reachability :

    @interface MyReachability : Reachability
    {
    }
    +(BOOL) connectedToNetwork;
    @end

    @implementation MyReachability

    + (BOOL) connectedToNetwork {
        // Create zero addy
        struct sockaddr_in zeroAddress;
        bzero(&zeroAddress, sizeof(zeroAddress));
        zeroAddress.sin_len = sizeof(zeroAddress);
        zeroAddress.sin_family = AF_INET;
    
        // Recover reachability flags
        SCNetworkReachabilityRef defaultRouteReachability = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&zeroAddress);
        SCNetworkReachabilityFlags flags;
    
        BOOL didRetrieveFlags = SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags);
        CFRelease(defaultRouteReachability);
    
        if (!didRetrieveFlags)
        {
            return NO;
        }
    
        BOOL isReachable = flags & kSCNetworkFlagsReachable;
        BOOL needsConnection = flags & kSCNetworkFlagsConnectionRequired;
        if (needsConnection && (flags & kSCNetworkReachabilityFlagsIsWWAN)) {
            needsConnection = NO;
        }
        return (isReachable && !needsConnection) ? YES : NO;
    }
    

    @end
    In YourApplicationDelegate.h:

    @interface YourApplicationDelegate :NSObject
    {
         BOOL connectivity;
    }
    - (void) reachabilityChanged:(NSNotification *) note;
    - (BOOL) checkConnectivity;
    @end
    

    In YourApplicationDelegate.m add function initReachability
    call it to init your notifications from Reachability class.

    - (void) initReachability
    {
        MyReachability * reachability = [ MyReachability sharedReachability ];
    
        //[ reachability setHostName: [self hostName] ];
        [ reachability setNetworkStatusNotificationsEnabled: YES ];
    
        [self siteAvailable];
    
        NSNotificationCenter * notificationCenter = [ NSNotificationCenter defaultCenter ];
        [ notificationCenter addObserver: self
                                selector: @selector( reachabilityChanged: )
                                    name: @"kNetworkReachabilityChangedNotification"
                                  object: nil ];
    
        connectivity = [MyReachability testConnection:NO];
        if (!connectivity) {
            if (![MyReachability testConnection:NO]) {
                [self addConnectivityView];
            }
        }
    }
    
    
    - (BOOL) checkConnectivity
    {
        return connectivity;
    }
    
    - (void) reachabilityChanged:(NSNotification *) note
    {
        BOOL    newConnectivity = [MyReachability testConnection:NO];
        if (connectivity != newConnectivity) {
            connectivity = newConnectivity;
            [self siteAvailable];
            if (![MyReachability testConnection:NO]) {
                [self addConnectivityView];
            } else {
                [self removeConnectivityView];
                connectivity = YES;
                [[NSNotificationCenter defaultCenter] postNotificationName:@"kAppNetworkReachabilityChangedNotification" object:nil];
            }
        }
    }
    

    Pay attention, that @”kAppNetworkReachabilityChangedNotification” is a custom notification, that your view should subscribe to.

    In your view controller’s viewDidLoad function:

    -(void) viewDidLoad
    {
           // your customization
        NSNotificationCenter * notificationCenter = [ NSNotificationCenter defaultCenter ];
        [ notificationCenter addObserver: self
                                selector: @selector( reachabilityChanged: )
                                    name: @"kAppNetworkReachabilityChangedNotification"
                                  object: nil ];
    
    }
    
    
    - (void) reachabilityChanged: (NSNotification *) note
    {
        BOOL connectivity = [YourAppDelegate checkConnectivity];
    
        if (connectivity) {
               // update your view accordingly
        } 
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok so I am trying to get this app to show network error alert
I get this error when trying to register a new user. NoMethodError (undefined method
Some Background I have an iPhone app with three UIWebViews, each used to show
Trying to get Flurry analytics working in my iPhone app. I begin a timed
I'm trying to get my TODO comments to show up in the task list
I have a weird error I'm trying to debug with no luck. I have
I am trying to get the hang of Core Data, and I am running
I'm having some hard time trying to send an email from my app. I
I've been trying is to create a false entry into the browser (i.e. browserManager.setFragment(bb=1))
I am taking my first steps with Sencha touch. The results are just what

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.