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