Freeing iOS UIWebView resources after usage

为君一笑 提交于 2019-11-29 22:58:38

It's been several months since I asked this question and it seems no one knows any work around, so I'm just answering myself to give it some closure and, sadly, bad news to whoever is having this same issue.

We kept trying to fight this issue for several days and, after talking to some people who somehow hinted us that we had nothing to do about it (it's just a different process taking care of everything to which we have no access), we decided to restrict the webview access to custom made pages.

For example, we had a news sections which opened webs on the internet. What we ended up doing was open custom-made web pages which contained the title, body and an image related to the news (which still adds to resource memory usage, but we managed to keep it in a contained small format which would need thousands of news to be opened to be of any danger). I know, not ideal.

I'd still love to see some constructive answers to this question, whether it's a solution, a workaround, or any idea that can help future users with this same issue have clues on how to deal with it.

you should read this article from Jason Baker,

http://www.codercowboy.com/code-uiwebview-memory-leak-prevention/

He has created a category which really helped me to redue memory footprint of UIWebView.

you just have to call two lines after adding category

-(void) dealloc
{

    [self.webview cleanForDealloc];
    self.webview = nil;
    [super dealloc];

}
override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    categoryWebView.stopLoading()
    categoryWebView.removeFromSuperview()
    categoryWebView = nil

    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)
        }
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!