0

I've implemented Reachability classes following this answer. I've two different problems, and I hope for your help...

I've put the notifier in my viewDidLoad:

(void)viewDidLoad
{
    [super viewDidLoad];
    // 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];
}

and I check internet connection in my viewDidAppear:

(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    //check connection when access to the view
    if (internetActive)
    {
        [self loadData];
    }
    else
    {
        UIAlertView * errorAlert = [[UIAlertView alloc] initWithTitle:@"Error loading content" message:@"Internet connection not available" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [errorAlert show];
    }
}

FIRST PROBLEM: HOW TO CHECK INTERNET CONNECTION FOR THE FIRST TIME

The first time I access to the view, the alert is displayed, also if the internet connection is ok. This, I think, because the notifier didn't already receive the notification about the connection. How to manage this?

SECOND PROBLEM: IF THE CONNECTION STATUS CHANGES (i.e. FROM ABSENT TO PRESENT), THE NOTIFIER DOESN'T ADVICE ABOUT THIS

If I start the app without internet connection, the alert is correctly displayed. But if the internet is available again, each time I access to the view the alert continues to be displayed, since the change of internet availability is not intercepted. I think this is due to the execution of the notifier on the same thread. How to implement this connection check in order to intercept also connection status changes?

Thanks in advance and Happy New Year, yassa

Community
  • 1
  • 1
yassassin
  • 3,185
  • 6
  • 28
  • 31

1 Answers1

0

Your suspission is correct, it is very usuall for the viewDidLoad and viewDidAppear methods to be called in the same run loop, which means that you will not receive any notification before viewDidAppear.

edit
I am not sure if this will work because I don't have xcode here right now:

Try to use [internetReachable currentReachabilityStatus] and [hostReachable currentReachabilityStatus] to check the current status before getting any notifications.

If your code is still the same as the answer you linked just call [self checkNetworkStatus:nil] in your viewDidLoad or viewDidAppear before checking the internetActive flag

Community
  • 1
  • 1
João Portela
  • 6,338
  • 7
  • 38
  • 51
  • Thank you very much for your hint!!! Changing `- (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; //check connection when access to the view //if (internetActive) if ([internetReachable currentReachabilityStatus]) {` worked fine for me!!! – yassassin Jan 01 '12 at 12:39