Question

I have a TableView that recieves data from a server and all works fine.

- (void)viewDidLoad
{
    [super viewDidLoad];

    // retrieveData is a method that Loads the Content
    [self retrieveData];

}

But If the user has no internet, the app crashs. How Can I check this connectivity for, if is Ok I load the data.. and If IS NOT OK, i send a NSAlert to the user?

Was it helpful?

Solution 2

Once you have downloaded and imported Reachbility.m and Reachbility.h files

create a helper function:

-(BOOL)IsConnected{
  Reachability *reachability = [Reachability reachabilityForInternetConnection];
  NetworkStatus networkStatus = [reachability currentReachabilityStatus];

  return !(networkStatus == NotReachable);    
}

Then use it

if([self IsConnected]){
 [self retrieveData];
}
else{
  //not connected to internet!
}

OTHER TIPS

You can check for internet connection as given below and hope you can implement UIAlertview on yours.

// Checks if we have an internet connection or not
- (void)testInternetConnection
{   
    internetReachableFoo = [Reachability reachabilityWithHostname:@"www.google.com"];

    // Internet is reachable
    internetReachableFoo.reachableBlock = ^(Reachability*reach)
    {
        // Update the UI on the main thread
        dispatch_async(dispatch_get_main_queue(), ^{
            NSLog(@"Yayyy, we have the interwebs!");
        });
    };

    // Internet is not reachable
    internetReachableFoo.unreachableBlock = ^(Reachability*reach)
    {
        // Update the UI on the main thread
        dispatch_async(dispatch_get_main_queue(), ^{
            NSLog(@"Someone broke the internet :(");
        });
    };

    [internetReachableFoo startNotifier];
}

This is well explained by iWasRobbed here.

Courtesy:- https://stackoverflow.com/a/3597085/1865424

  1. Do a google search for the "Reachability" class from Apple.
  2. Make your code bullet proof enough so that it won't crash even if there is no internet.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top