UIAlertView connection error on a UIWebView help

Discussion in 'iOS Programming' started by troop231, Nov 12, 2011.

  1. troop231 macrumors 603

    Joined:
    Jan 20, 2010
    #1
    Hello, I'm trying to display a UIAlertView when there is a connection error/no internet connection in my UIWebView. I turned off my wifi so the simulator would get no internet, and the alert displayed nicely, however when you click refresh in the webview it doesn't pop up again.

    Here is a video of the issue:

    YouTube: Screen recording

    And here is my total UIwebview code:

    Code:
    #import "AlaskaFish.h"
    
    @implementation AlaskaFish
    
    @synthesize webView;
    @synthesize activityIndicator;
    @synthesize refreshButton;
    @synthesize backButton;
    @synthesize forwardButton;
    @synthesize backButtonT;
    @synthesize forwardButtonT;
    @synthesize stopButton;
    @synthesize toolbarStop;
    @synthesize toolbarRefresh;
    
    
    
    - (BOOL)shouldAutorotateToInterfaceOrientation:
    (UIInterfaceOrientation)interfaceOrientation {
    	return (interfaceOrientation !=
    			UIInterfaceOrientationPortraitUpsideDown);
    }
    
    // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
    - (void)viewDidLoad {
        [super viewDidLoad];
    	self.title = @"Alaska";
    	[webView addSubview: activityIndicator];
    	NSString *urlAddress = @"http://www.adfg.alaska.gov/index.cfm?adfg=fishregulations.sport";
    	NSURL *url = [NSURL URLWithString:urlAddress];	
    	NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    	[webView loadRequest:requestObj];
        
        
    }
    
    
    
    -(void)actualizeButtons {
        backButton.enabled = [webView canGoBack];
        forwardButton.enabled = [webView canGoForward];
        backButtonT.enabled = [webView canGoBack];
        forwardButtonT.enabled = [webView canGoForward];
        
    }
    
    -(IBAction)refreshWebView {
        toolbarStop.hidden = NO;
        toolbarRefresh.hidden = YES;
        [webView reload];
        
        
    }
    
    -(IBAction)stop {
        toolbarStop.hidden = YES;
        toolbarRefresh.hidden = NO;
        [webView stopLoading];
        
        
    }
    
    -(IBAction)goBack {
        [webView goBack];
        [self actualizeButtons];
    }
    
    -(IBAction)goForward {
        [webView goForward];
        [self actualizeButtons];
    }
    
    
    - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error { NSLog(@"Did fail load with error: %@", [error description]); 
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Connection Failed" message:@"Check your Internet connection before refreshing." 
                                                       delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
        [alert show]; 
        [alert release]; 
        
        [activityIndicator stopAnimating];
        toolbarStop.hidden = YES;
        toolbarRefresh.hidden = NO;
        
        
        
    }
    
    - (void)webViewDidFinishLoad:(UIWebView *)webView {
        [activityIndicator stopAnimating];
        toolbarStop.hidden = YES;
        toolbarRefresh.hidden = NO;
        
        [self actualizeButtons];
    }
    
    - (void)webViewDidStartLoad:(UIWebView *)webView { 
        [activityIndicator startAnimating];
        toolbarStop.hidden = NO;
        toolbarRefresh.hidden = YES;
        
        
        
        
        
    }
    
    
    
    
    - (void)didReceiveMemoryWarning {
    	// Releases the view if it doesn't have a superview.
        [super didReceiveMemoryWarning];
    	
    	// Release any cached data, images, etc that aren't in use.
    }
    
    - (void)viewDidUnload {
    	// Release any retained subviews of the main view.
    	// e.g. self.myOutlet = nil;
    }
    
    
    - (void)dealloc {
        [super dealloc];
        [refreshButton release];
        [backButton release];
        [forwardButton release];
        [backButtonT release];
        [forwardButtonT release];
        [stopButton release];
        [activityIndicator release];
        [toolbarStop release];
        [toolbarRefresh release];
        [webView release];
        
    }
    
    @end
    My issue seems to be related perhaps that the didfailloadwitherror isn't recognized when I'm refreshing the initial loaded web page.

    Thanks for any help offered!
     
  2. jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #2
    Try using the request with TimeOut and Caching policy.
    Might be that your proxy or something similair is caching it out of your reach..
    It works fine for me, when I assign the UIWebviewDelegate.
     
  3. troop231 thread starter macrumors 603

    Joined:
    Jan 20, 2010
    #3
    Here's my new code, which is still not working:

    Code:
    #import "AlaskaFish.h"
    
    @implementation AlaskaFish
    
    @synthesize webView;
    @synthesize activityIndicator;
    @synthesize refreshButton;
    @synthesize backButton;
    @synthesize forwardButton;
    @synthesize backButtonT;
    @synthesize forwardButtonT;
    @synthesize stopButton;
    @synthesize toolbarStop;
    @synthesize toolbarRefresh;
    
    
    
    - (BOOL)shouldAutorotateToInterfaceOrientation:
    (UIInterfaceOrientation)interfaceOrientation {
    	return (interfaceOrientation !=
    			UIInterfaceOrientationPortraitUpsideDown);
    }
    
    // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
    - (void)viewDidLoad {
        [super viewDidLoad];
    	self.title = @"Alaska";
    	[webView addSubview: activityIndicator];
    	NSString *urlAddress = @"http://www.adfg.alaska.gov/index.cfm?adfg=fishregulations.sport";
    	NSURL *url = [NSURL URLWithString:urlAddress];	
    	NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    	[webView loadRequest:requestObj];
    }
    
    -(void)actualizeButtons {
        backButton.enabled = [webView canGoBack];
        forwardButton.enabled = [webView canGoForward];
        backButtonT.enabled = [webView canGoBack];
        forwardButtonT.enabled = [webView canGoForward];
    }
    
    
    -(IBAction)refreshWebView {
        
        if(webView.request != nil)
            [webView reload];
        else {
            // reconstruct requestObj here, or use a class member
            NSString *urlAddress = @"http://www.adfg.alaska.gov/index.cfm?adfg=fishregulations.sport";
            NSURL *url = [NSURL URLWithString:urlAddress];	
            NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
            [webView loadRequest:requestObj];
        }
        
        toolbarStop.hidden = NO;
        toolbarRefresh.hidden = YES;
    }
    
    
    -(IBAction)stop {
        toolbarStop.hidden = YES;
        toolbarRefresh.hidden = NO;
        [webView stopLoading];
    }
    
    
    -(IBAction)goBack {
        [webView goBack];
        [self actualizeButtons];
    }
    
    
    -(IBAction)goForward {
        [webView goForward];
        [self actualizeButtons];
    }
    
    
    - (void)webViewDidStartLoad:(UIWebView *)webView { 
        [activityIndicator startAnimating];
        toolbarStop.hidden = NO;
        toolbarRefresh.hidden = YES;
    }
    
    
    - (void)webViewDidFinishLoad:(UIWebView *)webView {
        [activityIndicator stopAnimating];
        toolbarStop.hidden = YES;
        toolbarRefresh.hidden = NO;
        [self actualizeButtons];
    }
    
    
    
    - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error { NSLog(@"Did fail load with error: %@", [error description]); 
        [activityIndicator stopAnimating];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No connection or server error" message:@"Please try again" 
                                                       delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
        [alert show]; 
        [alert release]; 
        
        
        toolbarStop.hidden = YES;
        toolbarRefresh.hidden = NO;
    }
    
    
    
    - (void)didReceiveMemoryWarning {
    	// Releases the view if it doesn't have a superview.
        [super didReceiveMemoryWarning];
    	
    	// Release any cached data, images, etc that aren't in use.
    }
    
    - (void)viewDidUnload {
    	// Release any retained subviews of the main view.
    	// e.g. self.myOutlet = nil;
    }
    
    
    - (void)dealloc {
        [super dealloc];
        [refreshButton release];
        [backButton release];
        [forwardButton release];
        [backButtonT release];
        [forwardButtonT release];
        [stopButton release];
        [activityIndicator release];
        [toolbarStop release];
        [toolbarRefresh release];
        [webView release];
        
    }
    
    @end
    If I load the page and THEN turn wifi off to the simulator, it actually works fine! The issue seems to be initially if it can't load a page, therefore not showing the UIAlert

    Edit: new screen recording: http://youtu.be/Oj3VVhGy570
     
  4. North Bronson macrumors 6502

    Joined:
    Oct 31, 2007
    Location:
    San José
    #4
    One thing to keep in mind is that you might have an internet connection, and still be called the failure method because some particular resource from the server might be acting weird. It is usually a good practice to explicitly check the error that is returned and display UI only for special occasions:

    Code:
    if ([error code] == NoInternetConnectionConstant)
    {
        //  Display UI
    }
    else
    {
        //  Log Error To Console
    }
     
  5. troop231, Nov 15, 2011
    Last edited: Nov 15, 2011

    troop231 thread starter macrumors 603

    Joined:
    Jan 20, 2010
    #5
    Thanks for the tip! Can you help with my code above?

    Edit: Nevermind! I'm still stuck!
     

Share This Page