如何使用UIWebView删除内存泄漏



我有一个uiwebview,在里面我必须加载一个URL。

问题是,在打开WebView后,发生了泄漏。我的意思是我无法删除内存泄漏。

以下是我的代码: -

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

我该怎么做才能删除内存泄漏?

谢谢

除了您的代码外,您需要获得这些要点:

  1. 第一件事是尝试不在主视图中添加WebView,而不是您可以设置Alpha或Hidden属性。如果您使用的是隐藏的属性,则在毫无疑问的情况下将其代表设置为nil并尝试管理该WebView在隐藏时无法在后台工作。

  2. 如果您在新的ViewController中显示,那么当我们推开WebView时,设置其代表并重新加载请求。现在,当您试图从该视图中回来时。在弹出之前,将其代表设置为零,将其设置为其中所有使用的本地变量。

例如:

在ViewDidload上:您正在设置委托,现在,当您弹出时,意味着移动到上一个屏幕使用以下代码:

  webView.delegate = nil
  webView.removeCache()

和,

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

和后面按钮:

@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)
    }
}

相关内容

  • 没有找到相关文章

最新更新