Adam Storr
Adam Storr

Reputation: 1450

iPhone - No Internet Connection Display

I'm working on an app that requires access to a webserver. I'm trying to display a view that will appear if there is no internet connection... much like you see on the Facebook app (link).

Any thoughts on how I can do this? Should I use a conditional statement and display a separate view instead?

Thanks in advance!!

Upvotes: 2

Views: 1727

Answers (1)

loafoe
loafoe

Reputation: 493

That is totally dependent on what type of navigation structure you App uses. Does it use tab bars? If so then you would want to display this message for all tab bars that require connectivity to be useful.

In my AppDelegate I always store the state of reachability (see http://developer.apple.com/library/ios/#samplecode/Reachability/Introduction/Intro.html). i.e. the AppDelegate gets notified of any reachability changes and stores the latest state. I created a boolean method which returns true if there is net connectivity:

- (BOOL)reachable;

Then, whenever I need to make decision to show the "no internet connection" I check the availability in the viewWillAppear method of the view controlller:

- (void) viewWillAppear:(BOOL)animated {
  [super viewWillAppear:animated];
  MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];

  if (![appDelegate reachable]) {
     // Add a subview that displays the "no internet connection" message
  } else {
     // Do your normal application flow..
  }
}

Upvotes: 3

Related Questions