I have read
this question and found its answer to be useful to check an internet connection for the iPhone. I implemented the check in my viewDidLoad message.
However my App does load Data from an XML file from a server (within viewWillAppear). In this case the check has to be done before the app tries to load the data from the internet source. My problem here is that the network check requires some seconds but the app does proceed with the code.
This results in delaying the check. So there is a valid internet connection, but the bool variable that stores this information is set too late.
Code in viewDidLoad
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkNetworkStatus:) name:kReachabilityChangedNotification object:nil];
internetReachable = [[Reachability reachabilityForInternetConnection] retain];
[internetReachable startNotifier];
The selected message is implemented in the same way like in the example in the link above. Additionally it sets some bool-properties so I can access the connection state within the app.
Code in viewWillAppear
if(internetActive == TRUE)
{
NSLog(@"connected");
//Code to load XML stuff
}
else
{
NSLog(@"not connected");
}
The console shows me "not connected". But a few seconds later the debug logs within "checkNetworkStatus" show me that the internet connection is available and the bool "internetActive" would be set.
I assume that the network check delivers his report about the connection state too late for my purpose. Is there a way to delay the app until the connection is established? Or would this lead to crashing the app cause the view isn't displayed within a few seconds?
Are there any other possibilities to ensure the connection?
My second question is how to ensure a specific internet address is available? I the link above there is mentioned
hostReachable = [[Reachability reachabilityWithHostName: @"www.apple.com"] retain];
[hostReachable startNotifier];
to detect the connection to a host, but I want to go in detail and try to detect if the host deserves a specific address or not. But entering the full address path (e.g. "localaddress.mylan.de/lan/xml/") instead of the host leads to a not reachable connection state for the host. Entering the mentioned address in the browser address bar shows me the correct result. I guess I have to check it otherwise but how?
Thanks for reading :)