0

I have an app that cannot load anything if it does not have access to the internet. In my app delegate I use this code:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

    //Check for internet
    internetReachableFoo = [Reachability reachabilityWithHostname:@"www.google.com"];

    // Internet is not reachable
    internetReachableFoo.unreachableBlock = ^(Reachability*reach)
    {
        // Update the UI on the main thread
        UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"This is an example alert!" delegate:self cancelButtonTitle:@"Hide" otherButtonTitles:nil];
        alert.alertViewStyle = UIAlertViewStylePlainTextInput;
        [alert show];
        exit(0);
        dispatch_async(dispatch_get_main_queue(), ^{
            NSLog(@"Someone broke the internet :(");

        });
    };
    [internetReachableFoo startNotifier];
    return YES;
}

The checking for the internet is done from this response Link and Then I want to show an alert box that tells the user it wont work without internet and then close the app. Currently I have the exit(0); which I am aware that apple wont accept therefore on my alert view I am needing the ok button to close the app (if apple allows that). My problem is I am not sure how to close an app especially with a UIAlert view (I am fairly new to iOS) and if that is the case to show the alert view, I want the app to stop loading everything else when the alert view is up if it doesn't have internet because the app will just crash anyway (again if apple allows you to stop the app from continuing to load) How can I go about this in an apple approved way?

Community
  • 1
  • 1
Grant Wilkinson
  • 1,088
  • 1
  • 13
  • 38
  • 1
    From a user experience perspective, what you're trying to do I a bad idea. Notify the user that there is no internet and disable the functions of the app, but don't close the app. – JeffRegan Jul 30 '13 at 04:02
  • I agree with both the answers (SK9 and Michael Dautermann). Check Reachability somewhere other than the app delegate (root view controller maybe) and don't close your app. Just notify the user they need to connect to the internet and don't make any more network calls until the Reachability changes to connected. – JeffRegan Jul 30 '13 at 04:05
  • ok got it. Thank you! Ill just move it around to not perform any functions until it is connected and not close the app – Grant Wilkinson Jul 30 '13 at 04:13

2 Answers2

6

A few things here.

1) you should be checking for Reachability in somewhere other than "didFinishLaunchingWithOptions", maybe somewhere else like say... your first view controller?

If "didFinishLaunchingWithOptions" takes too long to return, iOS assumes (read for exception code: 0x8badf00d) that your app is dead and kills it.

2) If you do put your Reachability code into the first view controller, that is where you can bring up your UIAlertView really easily.

For example, add a "UIAlertView" instance variable (or ivar) to your view controller.

If it's null, continue doing whatever the view is doing. If it exists (i.e. if the internet is down and the alert is being displayed), pause everything else in the view controller until the user exits on their own accord.

Or.... reset things when the user brings the app back to the foreground and dismiss the alert view yourself and see if the internet came back up.

Amar
  • 13,202
  • 7
  • 53
  • 71
Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
1

Don't close the app. Apple will certainly reject this.

Also, it's usually better form to attempt the request all the same and handle the failure gracefully. Internet signals can be intermittent and one fail needn't ruin it for all.

Ken
  • 30,811
  • 34
  • 116
  • 155