If it's a large file, you can't/shouldn't use UIWebView
.
Why? I tried to show a document file (docx) with a couple of images and my application was crashing, after throwing a memory warning. The reason was simple. Although the file size was ~2.5 MB, the device didn't have enough RAM/memory to display all the bitmap images (that were embedded in the document). Debugging the issue using Instruments showed that the application memory was spiking from 30 MB to 230 MB. I imagine you're experiencing something similar.
Possible Solutions:
Don't allow the user to open large files on their mobile devices. Either that, or gracefully stop/halt the UIWebView
loading process when you receive a memory warning.
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
if ([self.webView isLoading]) {
[self.webView stopLoading];
}
}
Try using [UIApplication sharedApplication] openURL:]
method instead.
Try using UIDocumentInteractionController
instead.
UIDocumentInteractionController *documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:targetURL];
documentInteractionController.delegate = self;
BOOL present = [documentInteractionController presentPreviewAnimated:YES];
if (!present) {
// Allow user to open the file in external editor
CGRect rect = CGRectMake(0.0, 0.0, self.view.frame.size.width, 10.0f);
present = [documentInteractionController presentOpenInMenuFromRect:rect inView:self.view animated:YES];
if (!present) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error"
message:@"Cannot preview or open the selected file"
delegate:nil
cancelButtonTitle:NSLocalizedString(@"OK", nil)
otherButtonTitles:nil, nil];
[alertView show];
}
}
Note: I haven't tried opening a keynote file using the above mentioned methods. In order to use UIDocumentInteractionController
, you'll have to download the file first.
Hope this helps.