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()
/
}
}