2

I have a UIWebView and inside that I have to load a url.

Problem is that After opening webView Memory leak is happened. I mean I am not able to remove memory leak.

Here below is my code :-

import UIKit
import Toast_Swift


class WebViewController: UIViewController,UIWebViewDelegate {

//WebView
@IBOutlet weak var webView: UIWebView!

//URL
var strUrl : String? = nil

//Tag
var tag : Int! = 0

//ViewDidLoad
override func viewDidLoad() {
    super.viewDidLoad()

    //Delegate of web view
    webView.delegate = self
    //webView.stringByEvaluatingJavaScript(from: "localStorage.clear();")
    self.webView.loadRequest(URLRequest(url: URL(string: self.strUrl!)!))

    //Loading View
    self.view.makeToastActivity(.center)
}

//MARK :- Web view delegate.
func webViewDidFinishLoad(_ webView: UIWebView) {

    //ToastManager.shared.tapToDismissEnabled = true
    self.view.hideToastActivity()
}

//Button Back Action
@IBAction func btnBack(_ sender: Any) {


    if (tag == 1) {
        webView.delegate = nil
        self.strUrl = ""
        webView.removeCache()
        let gotoCreateView = self.storyboard?.instantiateViewController(withIdentifier: "CreateAccountView") as! CreateAccountView

        self.present(gotoCreateView, animated: true, completion: nil)

    } else {

        webView.delegate = nil
        self.strUrl = ""
        webView.removeCache()
        let gotoAboutUsView = self.storyboard?.instantiateViewController(withIdentifier: "AboutUsView") as! AboutUsView

        self.present(gotoAboutUsView, animated: true, completion: nil)
    }


    /*if (1 == tag)
    {
        webView.delegate = nil
        webView.removeCache()
        let gotoCreateView = self.storyboard?.instantiateViewController(withIdentifier: "CreateAccountView") as! CreateAccountView

        self.present(gotoCreateView, animated: true, completion: nil)

    }

    else
    {
        webView.delegate = nil
        webView.removeCache()
        let gotoAboutUsView = self.storyboard?.instantiateViewController(withIdentifier: "AboutUsView") as! AboutUsView

        self.present(gotoAboutUsView, animated: true, completion: nil)
    }*/
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    webView.delegate = nil
    webView.removeCache()
    webView.delegate = self
    self.webView.reload()
 }
}

extension UIWebView
{
 func removeCache()
 {
    URLCache.shared.removeAllCachedResponses()
    URLCache.shared.diskCapacity = 0
    URLCache.shared.memoryCapacity = 0

    if let cookies = HTTPCookieStorage.shared.cookies {
        for cookie in cookies {
            HTTPCookieStorage.shared.deleteCookie(cookie)
        }
    }
  }
}

What can I do to remove memory leak?

Thanks

Kishor Pahalwani
  • 1,010
  • 1
  • 23
  • 53

1 Answers1

2

Apart from your code, you need to get these points:

  1. The first thing is try to not add the webView in main view, instead of this you can set alpha or hidden property. If you are using hidden property, then on unhiding make their delegate set to nil and try to manage that WebView will not work in background when it is hidden.

  2. If you are showing in new ViewController, then when we are pushing the WebView, set their delegate and reload the request. Now when you are trying to back from that view. Before poping, set their delegate to nil, set nil to all the local variables that are used in it.

For Example:

On ViewDidLoad: you are setting delegate, Now when you pop, means move to previous screen use these lines of code:

  webView.delegate = nil
  webView.removeCache()

And,

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    webView.delegate = nil
    webView.removeCache() 
    webView.delegate = self
    self.webView.reload()
 }

}

And on Back button:

@IBAction func btnBack(_ sender: Any) {
    if (tag == 1) {
        webView.delegate = nil
        self.strUrl = ""
        webView.removeCache()
        let gotoCreateView = self.storyboard?.instantiateViewController(withIdentifier: "CreateAccountView") as! CreateAccountView

        self.present(gotoCreateView, animated: true, completion: nil)

    } else {

        webView.delegate = nil
        self.strUrl = ""
        webView.removeCache()
        let gotoAboutUsView = self.storyboard?.instantiateViewController(withIdentifier: "AboutUsView") as! AboutUsView

        self.present(gotoAboutUsView, animated: true, completion: nil)
    }
}
Gourav Joshi
  • 2,419
  • 2
  • 27
  • 45