Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS UIWebView - trap exceptions loading invalid file format

I have a UIViewController containing a UIWebView which I am using to view various documents. Occasionally a document may attempt to be loaded which is not supported by the UIWebView (e.g. old Excel 2.0 sheet). Currently this causes output in the debug window as follows :

exception: CPMessageException: The file format is invalid.

EXCEPTION CPMessageException: (null)

The webViewDidStartLoad method fires, but neither webViewDidFinishLoad or didFailLoadWithError fires.

The error doesn't crash the app, but I would like to trap this exception to provide my own message for the user. Can anybody suggest how to trap and handle this?

Many thanks, Jonathan

like image 650
Jonathan Wareham Avatar asked Nov 13 '22 06:11

Jonathan Wareham


1 Answers

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

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

The best way to go around this would be to use the shouldStartLoadWithRequest delegate method of UIWebView:

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

Here you could preemptively stop the file from being loaded into the web view and avoid a clueless blank screen.

(Am not sure if there is a way to read NSLog output on the fly so you could monitor it. But that is probably not a right way to do it!)

Update: Just filed a bug regarding this. Let us see what the Apple Developers have to say on this.

like image 104
Nandeep Mali Avatar answered May 18 '23 18:05

Nandeep Mali