3

I have a UIWebView and want to display an activity indicator while it is loading. And then hide it when the webViewDidFinishLoading. The only problem is that I am getting this NSURLErrorDomain error -999 thing going on. After searching around I found this fix which works to not display any error message but my webViewDidFinishLoading doesn't ever get called to get rid of my activity indicator and other stuff that I have going on. I guess I could just make a call to the didFinishLoading method in my webViewDidFailWithError method if it fails with -999 but that seems super hacky and wrong. Any ideas on how to fix this?

edit* I have figured out where the webview was being asked to load twice so I was able to get rid of the error -999. However, it seems neither of the delegate methods are being called unless I try to load the webview twice (in which case the webViewDidFinishLoading method is only called once).

- (void)viewDidLoad
{
    self.webView.delegate = self;
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(refresh)
                                                 name:@"DidBecomeActive"
                                               object:nil];
    [super viewDidLoad];
}
-(void)refresh{
    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://myapp.com/app/"]]];
    self.webView.scrollView.bounces = NO;
}

- (void)webViewDidFinishLoading:(UIWebView *)wv
{
    NSLog(@"finished loading");
    [self.activityInd stopAnimating];
    [self.view bringSubviewToFront:wv];
}

- (void)webView:(UIWebView *)wv didFailLoadWithError:(NSError *)error
{
    NSLog(@"Failed: %@", error);
    if([error code] == NSURLErrorCancelled){
        return;
    }
    [self.activityInd stopAnimating];
    [[[UIAlertView alloc] initWithTitle:@"Can't find server" message:@"Check your internet connection and try again." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil]show];
}
Community
  • 1
  • 1
Chase Roberts
  • 9,082
  • 13
  • 73
  • 131

4 Answers4

13

Did u assigned the UIWebView delegates to its object like the following

webViewObject.delegate = self 
NSUserDefault
  • 1,794
  • 1
  • 17
  • 38
3

Copy/Paste error.

- (void)webViewDidFinishLoading:(UIWebView *)wv

Should be changed to:

- (void)webViewDidFinishLoad:(UIWebView *)wv
Chase Roberts
  • 9,082
  • 13
  • 73
  • 131
1

implemente the protocol "UIWebViewDelegate" in .h file

Naresh Gunda
  • 332
  • 1
  • 2
  • 12
0

In case you ended up here because you're wondering why your delegate methods aren't being called when the bug wasn't due to an incorrectly written delegate method:

Say your webview is normally instantiated via Interface Builder, but you forget to hook it up correctly (meaning it isn't instantiated). You can still set its delegate (e.g., self.webview.delegate = self;) without crashing or exceptions (provided self conforms to the UIWebViewDelegate protocol).

Similarly, you can load HTML into it (e.g., [self.webview loadHTMLString:someHTMLString baseURL:nil];) and the app will continue along happily without complaints. This would take you to an awkward situation where you've set the delegate, written the delegate methods correctly, loaded something into the webview, but your delegate methods will never fire.

So the advice for that situation: make sure you double check that your webview is connected properly (to the correct instance property/variable) in Interface Builder.

Thomas Verbeek
  • 2,361
  • 28
  • 30