gpt4 book ai didi

ios - 如何使用 UIWebView 消除内存泄漏?

转载 作者:可可西里 更新时间:2023-11-01 01:26:33 26 4
gpt4 key购买 nike

我有一个 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 或隐藏属性来代替它。如果您使用的是隐藏属性,那么在取消隐藏时将它们的委托(delegate)设置为 nil 并尝试管理 WebView 在隐藏时不会在后台运行。

  2. 如果您在新的 ViewController 中显示,那么当我们推送 WebView 时,设置他们的委托(delegate)并重新加载请求。现在,当您尝试从该 View 中返回时。在弹出之前,将它们的委托(delegate)设置为 nil,将 nil 设置为其中使用的所有局部变量。

例如:

在 ViewDidLoad 上:您正在设置委托(delegate),现在当你弹出时,意味着移动到上一个屏幕使用这些代码行:

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

关于ios - 如何使用 UIWebView 消除内存泄漏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41327353/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com