我已经尝试了所有这些(one,two,three,four)解决方案,但是当我从网络视图屏幕返回到之前的viewController后,它会冻结大约2秒钟(有时更多)。 viewWillAppear中没有任何内容会导致冻结。
以下是网络视图控制器的viewWillDisappear
:
-(void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[detailWebView stopLoading];
detailWebView.delegate = nil;
NSLog(@"viewWillDisappear called !!!");
}
第一个viewController:
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:41.0/255.0 green:151.0/255.0 blue:132.0/255.0 alpha:1.0]];
UIImage *imagePlus = [[UIImage imageNamed:@"create.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
buttonCreate = [UIButton buttonWithType:UIButtonTypeCustom];
[buttonCreate setImage:imagePlus forState:UIControlStateNormal];
[buttonCreate addTarget:self action:@selector(createActivity:) forControlEvents:UIControlEventTouchUpInside];
buttonCreate.frame = CGRectMake(self.view.frame.size.width - 40, 10, 16, 16);
[self.navigationController.navigationBar addSubview:buttonCreate];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"back", nil) style:UIBarButtonItemStylePlain target:nil action:nil];
[backButton setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor blackColor]} forState:UIControlStateNormal];
self.navigationItem.backBarButtonItem = backButton;
[self.navigationController.navigationBar setTintColor:[UIColor blackColor]];
}
更新:我刚刚确认如果我让webView完全加载然后转到上一个viewController,则没有观察到冻结。
答案 0 :(得分:4)
我执行以下操作,类似于@Mrunal建议但加载有效页面。我最初添加这个来处理这样一种情况:加载的页面有一个定时器,从UIWebView弹出后继续运行。对我来说这很有效。
- (void) viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
// Load a blank page as found things hang around.
[self.theWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"about:blank"]]];
}
注意我不会调用stopLoading
因为停止加载请求。这可能是Mrunal解决方案出了什么问题?
答案 1 :(得分:3)
在viewWillDisappear
:
-(void)viewWillDisappear:(BOOL)animated {
[detailWebView setDelegate:nil];
[detailWebView loadRequest: [NSURLRequest requestWithURL: [NSURL URLWithString:@""]]];
[detailWebView stopLoading];
[super viewWillDisappear:animated];
}
答案 2 :(得分:3)
您是否正在使用NSURLProtocol拦截来自UIWebView的Web请求?以下答案是根据该假设编写的。
加载网页涉及发送多个资源(图片,视频,HTML,JavaScript)的请求。每个资源都有一个将被NSURLProtocol
拦截的URL现在,如果在加载所有资源之前关闭webView,NSURLProtocol有时会取消对其拦截的资源的所有打开请求,并在此过程中冻结您的应用程序。
在退出webView之前,您将从NSURLProtocol取消注册。所以
override func viewWillDisappear{
NSURLProtocol.unregisterClass(/*your custom url protocol class name here*/)
}
应该解决冻结问题
PS:有时您可能为整个应用程序定义了NSURLProtocol,在这种情况下,我认为最好将UIwebView的NSURLProtocol与应用程序的其余部分分开。如果这对您来说是一个可行的选择,我对您的项目了解不多,
答案 3 :(得分:3)
我认为webview可能仍会在后台处理。而且我的体验是完全发布UIWebView非常烦人。你可能想看看Profiler:up并关闭webview的ViewController几次,缓冲区应该稳步增加。
这就是为什么我开始将多个解决方案方法从堆栈溢出合并到最终并完全从webview中删除所有存储并停止运行。
至关重要的是,webview设置为nil,以便真正释放它的内存,在过去我们使用了释放方法,如果你正确使用ARC,它现在已经“过时了”。
- (void)releaseWebView:(UIWebView *)webView
{
/*
There are several theories and rumors about UIWebView memory leaks, and how
to properly handle cleaning a UIWebView instance up before deallocation. This
method implements several of those recommendations.
#1: Various developers believe UIWebView may not properly throw away child
objects & views without forcing the UIWebView to load empty content before
dealloc.
Source: http://stackoverflow.com/questions/648396/does-uiwebview-leak-memory
*/
[webView loadHTMLString:@"" baseURL:nil];
/*
#2: Others claim that UIWebView's will leak if they are loading content
during dealloc.
Source: http://stackoverflow.com/questions/6124020/uiwebview-leaking
*/
[webView stopLoading];
/*
#3: Apple recommends setting the delegate to nil before deallocation:
"Important: Before releasing an instance of UIWebView for which you have set
a delegate, you must first set the UIWebView delegate property to nil before
disposing of the UIWebView instance. This can be done, for example, in the
dealloc method where you dispose of the UIWebView."
Source: UIWebViewDelegate class reference
*/
[webView setDelegate:nil];
/*
#4: If you're creating multiple child views for any given view, and you're
trying to deallocate an old child, that child is pointed to by the parent
view, and won't actually deallocate until that parent view dissapears. This
call below ensures that you are not creating many child views that will hang
around until the parent view is deallocated.
*/
[webView removeFromSuperview];
webView = nil;
}
请尝试在viewWillDisappear中调用此方法: