Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

App gets memory warnings and crashes when loading HTML file with many YouTube videos in UIWebView

I’m using a UIWebView in my app. It normally works well, but there is a situation in which the app receives a memory warning and finally crashes.

I load the content with this:

[self.webView loadHTMLString:htmlString baseURL:baseURL];

There is one case in which htmlString has inside over 25 YouTube videos (this is not my idea — it’s the web page I’m receiving). So in this case, the app receives some memory warnings and finally crashes.

How can I manage this situation? Is it possible to load the HTML file in various steps?

I don’t know if this has anything to do with it, but I’m setting the UIWebView size — and also the content size of a scroll view that encloses the web view — dynamically. This is the code:

- (void) webViewDidFinishLoad:(UIWebView *)webView
{
    // Adaptamos las vistas al contenido y ocultamos el indicador de actividad

    // Ponemos el webView del tamaño justo del contenido

    CGRect frame = webView.frame;
    // Hace falta cambiar la height porque si no, no coge los cambios. Visualmente no se ve diferencia
    frame.size.height = 1;
    webView.frame = frame;
    CGSize fittingSize = [webView sizeThatFits:CGSizeZero];
    frame.size.height = fittingSize.height;
    webView.frame = frame;

    // Movemos el botón y lo ponemos donde acabe el webView
    CGRect buttonFrame = self.visitSiteButton.frame;
    buttonFrame.origin.y = frame.origin.y + frame.size.height + 20;
    self.visitSiteButton.frame = buttonFrame;

    // Ampliamos el contenSize del scrollview general para que quepa todo el webView
    self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width, self.visitSiteButton.frame.origin.y + self.visitSiteButton.frame.size.height + 10);
}

Thanks a lot,

Carlos

like image 914
Carlos Avatar asked Jan 14 '13 18:01

Carlos


2 Answers

How about a naughty little trick. Get the html of the page with the code as below:

NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url];
    NSURLResponse * response = nil;
    NSError * error = nil;
    NSData * data  = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString* newStr = [[NSString alloc] initWithData:theData
                                     encoding:NSUTF8StringEncoding];

Use regex to find video elements and replace with your custom image hyperlink tag >. Display the html string as:

[webView loadHTMLString: baseURL:nil];
like image 114
Omar Avatar answered Nov 09 '22 09:11

Omar


Tough situation. If you're confident your code is not the cause (use instruments to check for leaks); then your next best bet may be to try and avoid the crash. A few possibilities come to mind:

1) When you receive the memory warning, you could potentially:

if (self.webView.loading) {
    [self.webview stopLoading];
    //inform user page cannot be fully loaded.
}

2) If that doesn't work, you might be able to return NO in UIWebViewDelegate webView:shouldStartLoadWithRequest:navigationType: following a memory warning to stop the page from continuing to pull in heavy resources.

3) Perhaps as a last resort, stop webview load, remove it from your view, and release it.

Hope one of those will work. Can you share the page that's crashing you? Sounds helpful for testing purposes! Also - you should file a rdar with Apple in case they'd like to address this at a lower level.

like image 32
Dave Avatar answered Nov 09 '22 07:11

Dave