Reputation: 51
class MyViewController: UIViewController {
@IBOutlet weak var webView: UIWebView?
override func viewDidLoad() {
super.viewDidLoad()
let url = NSURL(string: urlString)
let request = NSURLRequest(URL: url!)
SVProgressHUD.show()
webView?.loadRequest(request)
webView?.scrollView.header = MJRefreshNormalHeader(refreshingBlock: {
[weak self] in
if let strongSelf = self {
strongSelf.webView?.reload()
}}) }
override func viewDidDisappear(animated: Bool) {
super.viewDidDisappear(animated)
SVProgressHUD.dismiss() } }
extension MyViewController: UIWebViewDelegate {
func webViewDidFinishLoad(webView: UIWebView) {
webView.scrollView.header.endRefreshing()
SVProgressHUD.dismiss() } func webView(webView: UIWebView, didFailLoadWithError error: NSError?) {
webView.scrollView.header.endRefreshing()
SVProgressHUD.dismiss() } }
The view controller was pushed by a navigation controller, when I pop it, I got leaks. In instrument.Leak I saw these.
Leaked Object # Address Size Responsible Library Responsible Frame NSMutableArray 1 0x137a6ddb0 48 Bytes UIKit -[_UIKeyboardTextSelectionGestureController init]
_UIKeyboardTextSelectionController 1 0x137a6e800 96 Bytes UIKit -[UIWebSelectionAssistant addNonEditableForceTextSelectionGestureRecognizersToView:]
_UIKeyboardBasedNonEditableTextSelectionGestureController 1 0x137a6dcd0 160 Bytes UIKit -[UIWebSelectionAssistant addNonEditableForceTextSelectionGestureRecognizersToView:]
I'm sure that the webView, myViewController were delayed, but when pop the myViewController, 4M increased and not release. Please help and thanks.
List item
Upvotes: 3
Views: 2937
Reputation: 2492
According to an answer posted here, there is a workaround that if you set configuration.selectionGranularity
to WKSelectionGranularityCharacter
, the leaks stop:
let config = WKWebViewConfiguration()
config.selectionGranularity = .character //WKSelectionGranularityCharacter
let myWebview = WKWebview(frame: frame, configuration: config)
This worked for me, but then, when selecting text, there was no selection rectangle in the webview. This may or may not be a viable workaround in your case.
Edit I just noticed your question is for UIWebView, not WKWebView. It doesn't look like you can set this on UIWebView. I'll leave this answer for now since WKWebView folks googling this memory leak will probably find this thread...
Upvotes: 3