Reputation: 3444
I have an app that relies on a network connection being available. I have implemented reachability code to check for a network and/or host. However, my question is this… if I do this check in didFinishLaunchingWithOptions can i just put up an alert indicating to the user that the app needs a network and then gracefully exit the app? I have read mixed info on this and am unsure of how I can exit the app in a way such that Apple is happy and does not reject our app.
Upvotes: 0
Views: 2050
Reputation: 147
Look for Reachability.h in https://github.com/tonymillion/Reachability.
Once you've implemented Reachability.h:
Reachability *reachability = [Reachability reachabilityWithHostname:@"www.google.com"];
reachability.reachableBlock = ^(Reachability *reachability) {
[[NSUserDefaults standardUserDefaults] setObject:@"YES" forKey:@"IsConnected"];
NSLog(@"Network is reachable.");
};
reachability.unreachableBlock = ^(Reachability *reachability) {
[[NSUserDefaults standardUserDefaults] setObject:@"NO" forKey:@"IsConnected"];
NSLog(@"Network is unreachable.");
};
[reachability startNotifier];
Each block will run every time the mobile connects or disconnects to the internet.
Hope it helps
Upvotes: 1
Reputation: 12132
Is there a reason you need to exit the app for the user?
Instead of an alert, why not just add a UIView with messaging explaining why your app won't work without a connection, and let the user exit themselves.
Exiting for the user is pretty aggressive and will probably look like your app crashed.
Upvotes: 1
Reputation: 318804
If your app can't do anything at all unless there is a network connection, then show the alert view with no buttons.
But have your app deal with a network connection becoming available. If one becomes available, automatically dismiss the alert view and let the app run normally.
There is no need to force the app to terminate.
Update - based on some comments:
It would be much better to put the reachability check in the main view controller. Then it can be smart enough to deal with a lack of network. This handles no initial network as well as the network becoming unavailable as the app runs.
Upvotes: 2