0

To check internet connection in my iOS app I made the following:

import SystemConfiguration.framework

add Reachability.h and Reachability.m from https://developer.apple.com/library/ios/samplecode/Reachability/Introduction/Intro.html in my project

then I added this in ViewController.m:

#import <SystemConfiguration/SystemConfiguration.h>
#import "Reachability.h"

and

- (BOOL)connected
{
    Reachability *reachability = [Reachability reachabilityForInternetConnection];
    NetworkStatus networkStatus = [reachability currentReachabilityStatus];
    return !(networkStatus == NotReachable);
}
- (void)viewDidLoad {
    [super viewDidLoad];
    if (![self connected])
    {
        // not connected
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No internet connection!" message:@"Check internet connection!" delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil];
        [alert show];
    } else
    {        
    }
}

The alert appears with every view(controller) I open in my app. That nerves. It should only appear at one View (which requires internet connection) or just once (when starting the app or internet connection breaks). (Or any other ideas?)

Is there a way to do that?

  • 1
    Why? You could loose connection at any time. Check when you're actually needing to use the connection (and / or handle the connection error). – Wain Aug 19 '15 at 08:05
  • I don't really understand what you mean. "Check when you're actually needing to use the connection": how can I do that? –  Aug 19 '15 at 08:07
  • You are probably subclassing your ViewController class? Then get rid of the alert and only add it in the viewcontroller you want. – tafh Aug 19 '15 at 08:08
  • Add break point at `[alert show];`, and see who calls it. – tuledev Aug 19 '15 at 08:12
  • Check http://stackoverflow.com/a/31824568/2594560 – Ankita Shah Aug 19 '15 at 08:13
  • @anhtu I don't understand what you mean. Could you try to explain int to me? –  Aug 19 '15 at 08:29
  • I mean. Did you try do debug with a break point at this line `[alert show];` ? – tuledev Aug 19 '15 at 08:30

3 Answers3

0

I solved this issue by creating an own HomeViewController.h/HomeViewController.m file for the ViewController where my homepage is integrated. There I added the alert-if-no-internet-connection code.

0

Instead of the UIAlertView, you should put a boolean that makes true if you have internet connection and false if you don't and when you want to make a request, check boolean value and show alert or do what you want.

If you want to do this only once when ViewController is first time oppened you could do like this

- (BOOL)connected
{
    Reachability *reachability = [Reachability reachabilityForInternetConnection];
    NetworkStatus networkStatus = [reachability currentReachabilityStatus];
    return !(networkStatus == NotReachable);
}

- (void)viewDidLoad {
    [super viewDidLoad];
    if (![self connected])
    {
        if(hasPresentedAlert == false){

            // not connected
             UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No internet connection!" message:@"Check internet connection!" delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil];
             [alert show];
             hasPresentedAlert = true;
         }
    } else
    {        
    }
}

And in the interface declare the boolean

@interface ViewController (){
     BOOL hasPresentedAlert;
}
Silviu St
  • 1,810
  • 3
  • 33
  • 42
  • Thanks for your answer. What exactly is the different between my actual code an your "idea"? –  Aug 19 '15 at 09:34
  • Difference is that it will be showed only once and when you push another ViewController you won't need to implement Reachability methods because it will show alert from ViewController which will be in your stack. That's the "idea" – Silviu St Aug 19 '15 at 09:48
  • Thanks, I'll try it. Where to add "@interface ViewController (){ BOOL hasPresentedAlert; }" ViewController.h or ViewController.m? –  Aug 19 '15 at 10:26
  • in .h or .m where you see this line **@interface ViewController** add this line ** BOOL hasPresentedAlert;**. I recommend you to read or look to some tutorials to understand better. – Silviu St Aug 19 '15 at 10:29
0

in app delegate class you should write (BOOL)connected function,in view controller class you just access this function from app delegate.

-(void) viewDidLoad:(BOOL)animated{

[super viewDidLoad:animated];

AppDelegate * app=(AppDelegate *)[[UIApplication sharedApplication]delegate];

if ([app connected]) {

// not connected

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No internet connection!" message:@"Check internet connection!" delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil];

[alert show];

}

}