How to safely shut down a loading UIWebView in viewWillDisappear?


|
I have a view containing a UIWebView which is loading a google map (so lots of javascript etc). The problem I have is that if the user hits the 'back' button on the nav bar before the web view has finished loading, it is not clear to me how to tidily tell the web view to stop loading and then release it, without getting messages sent to the deallocated instance. I'm also not sure that a web view likes its container view disappearing before it's done (but I've no choice if the user hits the back button before it's loaded). In my viewWillDisappear handler I have this
this seems to handle most cases OK, as nil'ing the delegate stops it sending the didFailLoadWithError to my view controller. However if I release the web view in my view's dealloc method, sometimes (intermittently) I will still get a message sent to the deallocated instance, which seems to be related to the javascript running in the actual page, e.g.:
If I simply don't release the webview, then I don't get these messages though I guess I'm then leaking the webview. If I don't send the 'stopLoading' message, and simply release the webview within viewWillDisappear, then I see messages like this:
Possibly related, I sometimes (again totally intermittent) get an ugly heisenbug where clicking the back button on some other view's navbar will pop the title, but not the view. In other words I get left with the title of view n on the stack, but the view showing is still view n+1 (the result is you're trapped on this screen and cannot get back to the root view - you can go the other direction, i.e. push more views and pop back to the view that didn't pop corrrectly, just not to the root view. The only way out is to quit the app). At other times the same sequence of pushes and pops on the same views works fine. This particular one is driving me nuts. I think it may be related to the view disappearing before the web view is loaded, i.e. in this case I suspect it may scribble on memory and confuse the view stack. Or, this could be completely unrelated and a bug somewhere else (i've never been able to reproduce it in debug build mode, it only happens with release build settings when I can't watch it with gdb :-). From my debug runs, I don't think I'm over-releasing anything. And I only seem to be able to trigger it if at some point I have hit the view that has the web view, and it doesn't happen immediately after that. |
|||||||||||||||||||||||||
add comment (requires 50 reputation) |
|
A variation on this should fix both the leaking and zombie issues:
|
|||||||||||||||||||||||||||||||
|


|
The UINavigationController bug you're describing in the second part of your post might be related to your handling of memory warnings. I've experienced this phenomenon and I"ve been able to reproduce it on view n in the stack by simulating a memory warning while viewing view (n+1) in the stack. UIWebView is a memory eater, so getting memory warnings wouldn't be surprising when it's used as part of a view hierarchy.
|
|||||||
add comment (requires 50 reputation) |
|
A simple Your second problem sounds like a prematurely deallocated view, but I can't say much without seeing some code. |
|||||||
add comment (requires 50 reputation) |
|
There's a few ways to handle it, but this should work. You want the didFailLoadWithError message, it's what tells you it's stopped. Set a flag isLeaving=YES; Send the Webview a stopLoading. In didFailLoadWithError:, check for the error you get when the webview stops: if ((thiserror.code == NSURLErrorCancelled) && (isLeaving==YES)) { [otherClass performSelector:@selector(shootWebview) withObject:nil withDelay:0] } release the webView in shootWebview: variations: if you want to be cavalier about it, you can do the performSelector:withObject:withDelay: with a delay of [fillintheblank], call it 10-30 seconds without the check and you'll almost certainly get away with it, though I don't recommend it. You can have the didFailLoadWithError set a flag and clean it up somewhere else. or my favorite, maybe you don't need to dealloc it all when you leave. Won't you ever display that view container again? why not keep it around reuse it? Your debug being different from release issue, you might want to check your configuration to make sure that it's exactly the same. Bounty was on the reproducible part of the question, right? ;-). -- Oh wait a second, you might be taking a whole View container down with the WebView. You can do a variation on the above and wait to release the whole container in shootWebView. |
|||||||||||||||||||
add comment (requires 50 reputation) |
I have the same problem, when I'm use navigation controller with view controllers in stack > 2 and current view controller index > 2, if an memoryWarning occurs in this momens, it raises the same problems. There is inly 1 solution, which I found after many experiments with overriding pop and push methods in NavigationController, with the stack of view controllers, with views and superviews for stacked ViewControllers, etc.
It works fine for 3 view controllers in stack. |
|||
| add comment (requires 50 reputation) |
|
I had a similar problem to this using a UIWebView in OS3 - this description was a good starting point, however I found than simply nil'ing out the web view delegate before releasing the webView solved my problem. Reading the sample code (the accepted answer - above) - it seems like a lot of overkill. E.g. [webView release] and webView = nil lines do exactly the same thing given the way the author describes the variable is declared (so you don't need both). I'm also not fully convinced by all the retain and release lines either - but I guess your mileage will vary. |
|||
| add comment (requires 50 reputation) |
How to safely shut down a loading UIWebView in viewWillDisappear?的更多相关文章
- 简单的使用ehcache
之前一直感觉缓存是高上大的东西,没有心思去研究.做了之后发现,简单的使用还是很容易的.这里记录ehcache在jfinal中的简单使用. 1.ehcahe简介 EhCache 是一个纯Java的进程内 ...
- leveldb
[LevelDB] LevelDB is a fast key-value storage library that provides an ordered mapping from string k ...
- ehcache历史变迁及常用API的使用(转)
ehcache是一个用Java实现的使用简单,高速,实现线程安全的缓存管理类库,ehcache提供了用内存,磁盘文件存储,以及分布式存储方式等多种灵活的cache管理方案.同时ehcache作为开放源 ...
- iOS网络3—UIWebView与WKWebView使用详解
一.整体介绍 UIWebView自iOS2就有,WKWebView从iOS8才有,毫无疑问WKWebView将逐步取代笨重的UIWebView.通过简单的测试即可发现UIWebView占用过多内存,且 ...
- IOS UIWebView 下拉刷新功能的简单实现
1.运行效果图 2.swift 代码的实现 import UIKit class RefreshWebViewController: UIViewController,UIScrollViewDele ...
- 网络天荒地老之UIWebView&WebKit
UIWebView 是苹果提供的用来展示网页的UI控件,它也是最占内存的控件. iOS8.0之后出现了webkit框架,WKWebView相比UIWebView节省了1/4~1/3的内存,速度快,但是 ...
- UIWebView的使用
iOS中UIWebView的使用详解 一.初始化与三种加载方式 UIWebView继承与UIView,因此,其初始化方法和一般的view一样,通过alloc和init进行初始化,其加载数据的方式有三种 ...
- How to load a local .CSS file & JavaScript resources using iPhone UIWebView Class
This post will cover the basic setup and creation of an application with web content for iPhone that ...
- [iOS Hybrid实践:UIWebView中Html中用JS调用OC方法,OC执行JS代码]
原理: 1.JS调用OC 每次webview执行跳转时都会被iOS给拦截,执行下面函数获得系统允许. 因此可以根据跳转信息转给系统,执行相应功能,比如打开相册等. // 网页中的每一个请求都会被触发 ...
随机推荐
- IE 9 下的 css 陷阱
IE 9 下的 css 陷阱 今天 Karson 老大的分享. 根据说明 当 css 文件超过一定大小时会被自动截断. http://ju.outofmemory.cn/entry/168599
- python 函数名,闭包
1.函数名字的应用 函数名是什么? 函数名是函数的名字,本质:变量,特殊变量 函数名+() ———>执行此函数: 2.函数名的赋值: def func2(): print(44) f = fun ...
- php sprintf()
在写php代码的时候,有时候会用到sprintf()这个函数,那么它是怎么用的呢? 学习源头: http://www.w3school.com.cn/php/func_string_sprintf.a ...
- Linux 配置静态Ip地址
注:所有红色字体标注均为 Linux 的 操作命令 ! 1, 使用root账户登录系统 2, 可以先使用 ifconfig : 查看网卡信息 eth0 为默认的第一个网卡 , 如果有第二个就会显示 ...
- PHP5.3.8连接Sql Server SQLSRV30
PHP5.3连接SQL Server就不能用php_mssql.dll了. 网上下载了好多都不行,因为它的版本是5.2的,不能再PHP5.3中使用. 后来听说微软专门为PHP出了自己的dll. 叫做M ...
- C++对Lua中table进行读取、修改和创建
C++代码: // LuaAndC.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> #i ...
- C# 通用类型转换方法
在程序开发过程中经常需要进行数据的类型转换,而且如果一个字段的类型改成另一个类型时,所有相关的类型转换的地方都要跟着修改,不但造成了很多重复转换的代码而且修改字段类型时额外修改相关转换代码的工作量也很 ...
- Linux系统SCSI磁盘扫描机制解析及命令实例(转)
转载请在文首保留原文出处:EMC中文支持论坛 介绍 Linux系统扫描SCSI磁盘有几种方式?Linux新增LUN之后,能否不重启主机就认出设备?如果安装了PowerPath,动态添加/删除LUN的命 ...
- c# tcp udp 的使用场景
之前用tcp实现了一个案例(远程协助),后来我考虑用udp去实现它,于是又研究了下udp,我发现理论上udp可以做到,但是有一些问题不知道会不会有瓶颈 我参照网上写了一个简单的示例如下 服务端接收.发 ...
- 关于python3 发送邮件
一:发送文本信息 from email.mime.text import MIMEText from email.header import Header from smtplib import SM ...