2

I have a problem loading a large web page in a UIWebView where the connection drops for some reason trying to load a java script file. The load stalls at the point with neither didFailLoadWithError or webViewDidFinishLoad ever being invoked. The delegate is setup correct as when there is no networking error the page loads fine and the webViewDidFinishLoad method is invoked.

While trying to figure out why the page load stalls I implemented and registered a custom NSURLProtocol where I found out that connection delegate didFailWithError would be invoked most of the time, but the web view delegate would never be invoked after that.

I changed the custom protocol to run the connection in the main thread, which caused the didFailWithError method to be invoked every time the network connection dropped

- (void)startLoading {
  NSMutableURLRequest *newRequest = [self.request mutableCopy];
  [NSURLProtocol setProperty:@YES forKey:ProtocolKey inRequest:newRequest];


  self.connection  = [[NSURLConnection alloc]
                                initWithRequest:newRequest
                                delegate:self startImmediately:NO];

  [self.connection scheduleInRunLoop:[NSRunLoop mainRunLoop]
                      forMode:NSDefaultRunLoopMode];
  [self.connection start];   
}

The web view delegate webViewDidFinishLoad method was still not being called so I added a notification to the didFailWithError method, which allows me to handle the error in my web view controller

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
  NSDictionary *dict = @{ @"errorDescription":error.localizedDescription };


  [[NSNotificationCenter defaultCenter] postNotificationName:ErrorKey object:nil ];

  [self.client URLProtocol:self didFailWithError:error];
}

I can't figure out why it seems that the request being run by the web view on the background thread seem to get destroyed before the delegate methods can be invoked and the errors handled?

I was having similar issues with WKWebView, I assume with the same resource javascript, but I can not trap the errors in the custom protocol handler so I don't know what is going on there.

Has any one run into this before? Or have a better way of resolving this problem?

Jac
  • 21
  • 2
  • This link may start you down some path that helps. http://stackoverflow.com/questions/10996028/uiwebview-when-did-a-page-really-finish-loading – Matthew Carlson Jan 08 '16 at 01:44

0 Answers0