user1419810 user1419810 - 16 days ago 5
Objective-C Question

Show error message is internet not available

I'm loading a webview and am using SVProgressHUD to display a loading message whilst the page is loaded. Is it possible to show an error message if there isn't an internet connection available, if so how would i do this?

- (void)viewDidLoad
{
[super viewDidLoad];
_infowebView.delegate = self;
NSURL *url = [NSURL URLWithString:@"http://example.com"];
[self.infowebView loadRequest:[NSURLRequest requestWithURL:url]];
}

- (void)webViewDidStartLoad:(UIWebView *)webView {
[SVProgressHUD showWithStatus:@"Loading"];
}

Answer

Before loading the page you can check the internet connection using the reachability class:

+(BOOL)IsInternet
{
    Reachability *networkReachability = [Reachability reachabilityForInternetConnection];
    NetworkStatus networkStatus = [networkReachability currentReachabilityStatus];
    if (networkStatus == NotReachable)
    {
        return NO;
    }
    else
    {
        return YES;
    }
}

How to use:

if([className IsInternet]){
//Load the page
}else{
// Show message internet not available
}