5

I'm testing a UIWebview with a number of different document types - .xlsx, .jpg, etc. - and it opens most of them just fine. From time to time, I open a local file and this message appears right in the web view:

Unable to Read Document
An error occurred while reading the document

I'm not concerned with "why" this error occurs - it happens, for instance, when I feed the UIWebView a garbage file (intentionally). The problem is that I can't figure out how to detect "when" this happens. It doesn't trigger webView:didFailLoadWithError, it doesn't trigger an NSException (via @try & @catch), and when I inspect the document in webViewDidFinishLoad, webView.request.HTTPBody is null.

Anyone know how to detect when UIWebView can't display content?

cetcet
  • 2,147
  • 2
  • 15
  • 15
  • It's an old question but in my case `webView:didFailLoadWithError` worked for me in iOS 7/8. Using `stringByEvaluatingJavaScriptFromString` sometimes deliverd me data, but it was not the one I was looking for (old one? garbage file?). – testing Dec 16 '14 at 15:58

2 Answers2

3
just call uiwebview delegate method 

- (void)webViewDidStartLoad:(UIWebView *)webView
{

}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    NSString *html = [webView stringByEvaluatingJavaScriptFromString:
                      @"document.body.innerHTML"];
    if ([html rangeOfString:@"Unable to Read Document."].location == NSNotFound) {

       NSLog(@"NO Error");

    }
    else
    {
//        NSLog(@"File contains Error");
    }

}
Dipak Narigara
  • 1,796
  • 16
  • 18
0

If inspecting the log does not show anything out of the ordinary, run it on the actual device. As a bit of help, my log says

Cannot find data converter callback for uti public.data
Failed to generate preview

which means that the simulator is failing. After being frustrated with this same problem, I went thru the whole final certification and installation and provisioning process, and too confirm that Word 97 and Pages / Pages.zip files containing text do indeed display just fine on the actual device. SOLVED. The simulator itself is broken, which is very...troubling, that this little note didn't seem to make it into the release notes, and also complicates development a tad bit. However, the work around is to select the Device in Xcode and deploy out, and it should work.

PapaSmurf
  • 2,345
  • 1
  • 20
  • 10
  • 1
    Thanks for the reply, but that's not the issue. I'm not having trouble reading Word files. Rather, I'm having trouble detecting when UIWebView can't read any particular file. It has nothing to do with Simulator/Device discrepancies. – cetcet Mar 02 '11 at 19:59
  • usually check for a nil return, or a zero-length return. I was using the wrong encoding for a file (I saved it as Roman 8 not UTF) and the routine was not failing, but the string was 0 length. Took me 2 days to figure that one out. – PapaSmurf May 12 '11 at 17:28
  • @PapaSmurf: Who should return something? – testing Dec 16 '14 at 14:26