Search code examples
iosobjective-cuiwebviewuialertviewreachability

Implement a UIAlertView when a UIWebView does not have an internet connection


I am creating a rather simple tab-based app with a few UIWebViews. To pass App Store Approval, I need to implement an error if there is no network connection. I'm a beginner, but I tried this and nothing happened.

-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                                message:@"Can't connect. Please check your internet Connection"
                                               delegate:self
                                      cancelButtonTitle:@"OK"
                                      otherButtonTitles:nil];
[alert show];

}

I've tried Reachability, but its complicated, and I'm not sure exactly how to implement it into a UIAlertView. Could this code work with tweaking or is it trash?


Solution

  • You can check Network Connection using Reachability Code at ViewDidLoad or ViewWillAppear ..Download them using below link

    https://github.com/tonymillion/Reachability

        Reachability *reach = [Reachability reachabilityForInternetConnection];
        NetworkStatus netStatus = [reach currentReachabilityStatus];
        if (netStatus != NotReachable)
        {
         NSLog(@"Async.....Network is Available");
        }
        else
        {
     UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Network Error" message:@"No Network Available" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil , nil];
            [alertView show];
        }
    

    Or You can Put alert on below delegate method of UIWebView

    • (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error

    Hope it helps you...!