0

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 :)

Community
  • 1
  • 1
TRD
  • 1,027
  • 10
  • 20

2 Answers2

1

After getting more experiences with IOS development my first question is solved with three steps.

  1. First step is to start all necessary notifiers once. This can be done by starting them in viewDidLoad of the first loaded ViewController.
  2. Check current connection state by evaluating property named currentReachabilityStatus of reachability-objects. This state can be "not connected" even if a connection is available. This happens if the check is done within the ViewController who is starting the notifier. Reason for this is the time it takes while the notifier recognizes the connection. If currentReachabilityStatus property is evaluated as connected then data loading processes can be started.
  3. Evaluating the connection state within the method whose selector is added during adding the notifier. This method will start every time the connection state changes. First call will be directly after starting the notifier. If connection state is evaluated as connected then data loading processes can be started.

The second question stays unresolved up to now. May be there is no way other than sending a request to the target and waiting for a response or a timeout.

TRD
  • 1,027
  • 10
  • 20
0

Did you try dispatch_async & dispatch_sync(dispatch_get_main_queue( reload data here ) ?

Paradise
  • 528
  • 5
  • 17