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?