Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

troop231

macrumors 603
Original poster
Jan 20, 2010
5,826
561
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!
 
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.
 
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.

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
 
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
}
 
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
}

Thanks for the tip! Can you help with my code above?

Edit: Nevermind! I'm still stuck!
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.