IOS UIWebView - exception traps for loading invalid file format

I have a UIViewController containing a UIWebView that I use to view various documents. Sometimes a document may be loaded that is not supported by UIWebView (for example, an old Excel 2.0 worksheet). This currently causes output in the debug window as follows:

exception: CPMessageException: file format is not valid.

EXCEPTION CPMessageException: (null)

The webViewDidStartLoad method is started, but neither webViewDidFinishLoad nor didFailLoadWithError are started.

The error will not cause the application to crash, but I would like to catch this exception in order to provide my own message to the user. Can anyone suggest how to catch and handle this?

Thanks a lot Jonathan

+5
source share
1 answer

Unfortunately, CPMessageException is part of Apple's private frameworks, in the case of office files, OfficeImport.framework. This is thrown when the environment receives an unusable file.

The reason your application is not crashing is because it is probably being processed by the import code itself. Even setting NSSetUncaughtExceptionHandler will not work (as I tried in the past).

The best way around this is to use the shouldStartLoadWithRequest delegate method for the UIWebView:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType

Here you can proactively stop the file from being downloaded to the web view and avoid a blank screen.

(Not sure if there is a way to read the NSLog output on the fly so you can control it. But this is probably not the right way to do it!)

: . , Apple .

+1

All Articles