Swift: UIWebView crashing when loading HTML file with large content

Hello,


I am working on sample project and trying to load HTML file with large content into ‘UIWebView’ in ‘UIView’. When I try load normal size HTML file, the app works fine without any issues. But when tried to load HTML file with large content the app crashes due to high memory usage. Also observed that when app going to increase the height of ‘UIWebView’ as per the content height of HTML, its crashed. Tried to run on iPhone7 Plus with iOS 10.3.1v. However, it works on Simulator due to enough memory, but its crashing when I try to run against device.

Find below the sample which demonstrate high memory usage while loading HTML in "UIWebView". Please do let me know if there is any work around or solution to load HTML with large content without high memory usage. The html files are added in bundle. For sample HTML can be found at:

http://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html

************************************************************


class ViewController: UIViewController, UIWebViewDelegate {
    /
    /
    @IBOutlet var webView: UIWebView!

    /
    var bodyContentHeight:CGFloat = 0.0
    var heightConstraint: NSLayoutConstraint!
    /
    override func viewDidLoad()
    {
        super.viewDidLoad()
        /
     
        self.loadBodyInWebView()
    }


    /
    func webViewDidFinishLoad(_ webView: UIWebView)
    {
        UserDefaults.standard.setValue(0, forKey: "WebKitCacheModelPreferenceKey")
        Timer.scheduledTimer(timeInterval: 0.5, target: self, selector: #selector(updateForBodyHeight), userInfo: nil, repeats: false)
    }

    /
    func updateForBodyHeight()
    {
        self.bodyContentHeight = webView.scrollView.contentSize.height
        print("Body height : \(self.bodyContentHeight)")
        self.adjustWebViewHeight()
    }


    func loadBodyInWebView()
    {
        let url = Bundle.main.url(forResource: "testbody2", withExtension: "html")
        let request:URLRequest = URLRequest(url: url!)
        self.webView.loadRequest(request)
    }

    func adjustWebViewHeight()
    {
        var rect:CGRect=self.webView.frame
        rect.size.height=self.bodyContentHeight
        self.webView.frame=rect
     
        print("WebView height : \(self.webView.frame.size.height)")
        self.webView.translatesAutoresizingMaskIntoConstraints = false
        self.webView.topAnchor.constraint(equalTo: self.view.topAnchor).isActive = true
        self.webView.rightAnchor.constraint(equalTo: self.view.rightAnchor).isActive = true
        self.webView.leftAnchor.constraint(equalTo: self.view.leftAnchor).isActive = true
        self.webView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true
     
        self.heightConstraint = webView.heightAnchor.constraint(equalToConstant: self.bodyContentHeight)
        self.heightConstraint.isActive = true
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        /
    }
}

Is there a reason you’re using the older UIWebView rather than the newer, and generally recommended, WKWebView? This matters in this case because WKWebView does most of its work out of process, so if something goes wrong it doesn’t take down your entire app (-:

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Hello,


Thanks for your suggestion. However first I tried with WKWebView - but faced some issues related to rendering the text. So I again tried with UIWebView. Here the issue which I am facing is if we increase height of UIWebView as per content height of html content, then large memory consumed as per HTML content size. If that memory consumption exceeds device's max memory limit, then application crashes. Hope it hepls to understand the issue.

My point here is about mitigation, not functionality. Loading contents that tool big into a WKWebView will a) not crash your main app, and b) tell you when things go wrong (via the

webViewWebContentProcessDidTerminate(_:)
delegate callback). These are good things.

If that memory consumption exceeds device's max memory limit, then application crashes.

I’m not sure how you expect to be able to render a page that won’t fit into the device’s memory.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
Swift: UIWebView crashing when loading HTML file with large content
 
 
Q