5

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?

Jack Allen
  • 549
  • 2
  • 5
  • 19
rplankenhorn
  • 2,075
  • 2
  • 22
  • 32

2 Answers2

1

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.

Community
  • 1
  • 1
Balram Tiwari
  • 5,657
  • 2
  • 23
  • 41
  • Thanks for your answer. The actual problem is, the file has been already downloaded to the docs directory. With that file path, I have created url request and loaded the file in the webview. There only, it is crashing. – Ilanchezhian May 13 '14 at 06:28
  • I would say here that, to test this use case with some other file of 100+mb. Check if that opens. May be a PDF or some doc. It could be corrupted file that is leading to crash. – Balram Tiwari May 13 '14 at 06:33
  • To test this use case, I have already tested with a doc file too. And it is crashing. More importantly, the document is not corrupted. – Ilanchezhian May 13 '14 at 08:46
0

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:

  1. 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];        
        }
    }
    
  2. Try using [UIApplication sharedApplication] openURL:] method instead.

  3. 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.

Mustafa
  • 20,504
  • 42
  • 146
  • 209