Check internet connection iOS - Obj-C

240 views Asked by At

I am trying to check if my app has internet connection and I can do this using apples Reachability, like this

internetReach = [Reachability reachabilityForInternetConnection];
[internetReach startNotifier];
NetworkStatus netStatus = [internetReach currentReachabilityStatus];
witch (netStatus){
    case ReachableViaWWAN:{
        isReachable = YES;
        NSLog(@"4g");
        noInternetView.hidden = YES;
        break;
    }
    case ReachableViaWiFi:{
        isReachable = YES;
        noInternetView.hidden = YES;
        NSLog(@"wifi");
        break;
    }
    case NotReachable:{
        NSLog(@"NONE");
        noInternetView = [[CheckInternetView alloc] initWithFrame:self.view.bounds];
        [self.view addSubview:noInternetView];
        isReachable = NO;
        break;
    }
}

And it works, but now what I want to do is if it is the last case where there is no internet, I show a view telling the user and then in the background, I want to check for when the internet comes back and if it does come back then remove the view, like i did.

I tried putting the above code in a method, and if no internet then call the method again, but then the view just didn't appear and if wasn't running a check it just continually ran the method over and over.

So if there is no internet how can I run a check in the background to see when it comes back, and then hide the view?

Thanks for the help in advace.

EDIT

Here is my method I first call from viewDidLoad

-(void)checkInternet {

    internetReach = [Reachability reachabilityForInternetConnection];
    [internetReach startNotifier];
    NetworkStatus netStatus = [internetReach currentReachabilityStatus];

    switch (netStatus){
        case ReachableViaWWAN:{
            isReachable = YES;
            NSLog(@"4g");
            noInternetView.hidden = YES;
            break;
        }
        case ReachableViaWiFi:{
            isReachable = YES;
            noInternetView.hidden = YES;
            NSLog(@"wifi");
            break;
        }
        case NotReachable:{

            NSLog(@"NONE");
            noInternetView = [[CheckInternetView alloc] initWithFrame:self.view.bounds];
            [self.view addSubview:noInternetView];
            isReachable = NO;
            [self checkInternet];
            break;
        }
    }
}

And it works when I turn internet back on, I get the correct case, but when it is continuously running to check when the internet comes on, the view does not appear? Why doesn't it appear?

1

There are 1 answers

1
Aanabidden On

for background entry you can use applicationDidEnterForeground method to check the status in AppDelegate.

-(void)applicationDidEnterForeground:(UIApplication *)application {
    // call your method here to check internet status
    // lets say its
    [Network checkReachabilityStatus];
}

OR

you can use in the view UIApplicationDidBecomeActiveNotification notification to recheck internet status and hide/ show the view

-(void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showHideInternetStatusView) name:UIApplicationDidBecomeActiveNotification object:nil];
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];

    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil];
}