Reputation: 2084
I am trying to load a large Keynote file (~150MB) into a UIWebView
and I keep getting memory warnings and my app crashes.
Is there a way around this?
What is the proper way to open files that are this large without opening them in another app?
Upvotes: 5
Views: 682
Reputation: 20564
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.
Upvotes: 0
Reputation: 5667
When you open a file in UIWebView
directly from the url, the downloaded content is stored temporarily in RAM. The RAM is a shared space for the whole device, which has to perform other OS related tasks. Hence Your app is being killed by iOS due to memory pressure & resource crunch.
It is advisable to directly write your content into the file in NSDocumentsDirectory
in background & load the file in UIWebView
later.
With my knowledge I can suggest you the following.
The download Part
The Preview Part
Hope that helps.
Upvotes: 1