Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

DennisBlah

macrumors 6502
Original poster
Dec 5, 2013
485
2
The Netherlands
Hi all,

I'm working on an app that will load some file from the web and parse it into the app.

I have a problem, since I'm using Reachability class to know if the device is having a internet connection and if the host is reachable.

If one of these 2 are false, then I'm presenting an different view controller, with an anouncement that, the user has no internet, or that the server is down.

This is my code:
Code:
-(void)viewDidAppear:(BOOL)animated {
    [indicator startAnimating];
    CompanyappDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    
    internetReachable = [Reachability reachabilityForInternetConnection];
    [internetReachable startNotifier];
    hostReachable = [Reachability reachabilityWithHostName:@"www.mydomain.com"];
    [hostReachable startNotifier];
    
    loadData = [NSTimer scheduledTimerWithTimeInterval:.2 target:self selector:@selector(loadData) userInfo:nil repeats:NO];
}



-(void)loadData {
    if(internetActive && hostActive) {
        //Ok, we have connection and the host is alive.
        //Doing all my loading data in here.
    } else {
        BOOL deviceIsIpad = NO;
        if ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ) {
            deviceIsIpad = YES;
        }
        UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main_iPad" bundle:nil];
        if(!deviceIsIpad) {
            mainStoryboard = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle:nil];
        }
        UIViewController *nextView = [mainStoryboard instantiateViewControllerWithIdentifier:@"errorView"];
        [self presentViewController: nextView animated:YES completion:nil];
    }
}

It does work fine, however sometimes it does not work. The screen stays black. When I double tap the home button it does show me the 'errorView' but in app the screen stays black. In xCode i'm having this warning:

Code:
Warning: Attempt to present <dataView: 0x95ab070> on <connectionLoss: 0x97801a0> whose view is not in the window hierarchy!
 
Last edited:
Hi fabriciom,

thanks, but Im actually using this to create an dictionary of the xml files Im using from my webhost.

The problem is about loading the viewcontroller

As you see in my code above, I'm using reachability class to check if the device has an internet connection, and if the host is reachable.
If not, then load the connectionLoss view.

But the strange thing is, in connectionLoss I'm doing exactly same thing, however when both booleans are true it loads back to the main viewcontroller. However I get the warning at that moment and won't go further
 
Hi fabriciom,

thanks, but Im actually using this to create an dictionary of the xml files Im using from my webhost.

The problem is about loading the viewcontroller

As you see in my code above, I'm using reachability class to check if the device has an internet connection, and if the host is reachable.
If not, then load the connectionLoss view.

But the strange thing is, in connectionLoss I'm doing exactly same thing, however when both booleans are true it loads back to the main viewcontroller. However I get the warning at that moment and won't go further

Hello Dennis,

I suggest to use NSURLSession because its what is recommend by Apple (IOS 7). In another words instead of trying to see if you are connected or not just do your task and catch the error with the NSURLSession events. I think the reachability class is quite old.

Just my 2 cents...
 
Hello Dennis,
I suggest to use NSURLSession because its what is recommend by Apple (IOS 7). In another words instead of trying to see if you are connected or not just do your task and catch the error with the NSURLSession events. I think the reachability class is quite old.
Just my 2 cents...

I understand what you mean. And you are right, ofcourse. But it's not the problem I'm strugling with.

Still thanks for your tip about the connection check.:cool:
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.