ios:EXC_BAD_ACCESS for Webview delegate

泪湿孤枕 提交于 2019-11-29 02:26:12

The following might be the case here

  • The user is presented a screen with UIWebView
  • The UIViewController sets self as the delegate Web page starts downloading
  • The User quits screen
  • UIViewController gets deallocated UIWebView finishes loading and sends I am finished loading message to its delegate

or

some other delegate method gets called when the webview object is no more.i.e dangling pointer effect

1.Always make sure you stop loading the webView and remove the delegate before leaving the view

Before releasing an instance of UIWebView for which you have set a delegate, you must first set its delegate property to nil. This can be done, in your dealloc method

Here is the reference

// If ARC is used
- (void)dealloc {
    [_webView setDelegate:nil];
    [_webView stopLoading];
}

// If ARC is not used
- (void)dealloc {
    [webView setDelegate:nil];
    [webView stopLoading];
    [webView release];
    [super dealloc];
}

// ARC - Before iOS6 as its deprecated from it.
- (void)viewWillUnload {
    [webView setDelegate:nil];
    [webView stopLoading];
}

2.Make sure you are not stopLoading and setDelegate to nil in viewWillDisappear

if the ViewController is a child of a another ViewController, u can trigger the removal of the ViewController's view from the parent ViewController's view with an animation. At the same time, u can remove the ViewController from its parent and nil out its reference. at this point ViewController will be nil and viewWillDisappear will never be called, meaning the WebView delegate will never be cleaned up

Use dealloc and ensure that your WebView is always cleaned up.

3.Make sure you set the ContentOffset of the subviews of webview to CGPointZero without animation

In iPad in some versions while webview is scrolling if you close the parent viewcontroller without setting ContentOffset to CGPointZero this kind of problems will come

so its better to you call the following code of in parent viewcontroller before closing it

 for (id subview in webView.subviews){
        if ([[subview class] isSubclassOfClass: [UIScrollView class]]){
            [subview setContentOffset:CGPointZero animated:NO];
        }
    }

Hope this helps.Feel free to ask your doubts.

4.Generally speaking You should not embed UIWebView objects in UIScrollView objects. If you do so, unexpected behavior can result because touch events for the two objects can be mixed up and wrongly handled.

Here is the reference

Try disabling your UIWebView's scrolling behaviour before the ViewController deallocs it

for (id subview in webView.subviews){
    if ([[subview class] isSubclassOfClass: [UIScrollView class]]){
        [subview setContentOffset:CGPointZero animated:NO];
    }
}

p.s. Dipen Chudasama's approach is correct, according to Apple's documentation, you should really set the delegate property to nil before releasing the webview, assuming you have released the webview correctly inside dealloc function but not viewWillDisappear

UIWebViews delegate uses assign and not weak. So you need to nullify the delegate when the webView's controller gets deallocated.

Example:

- (void)dealloc
{
     self.webview.delegate =nil;
}

Are you adding a web view within your subclass of a web view? Generally that's the problem and if so, changing the superclass to a UIView would solve the problem.

Use visa versa means first nil your delegate and then after stop loading web view may be help you.

like this.

[_webView setDelegate:nil];
[_webView stopLoading];

As per apple document : Important Before releasing an instance of UIWebView for which you have set a delegate, you must first set its delegate property to nil.

iPhone developer.

Use [listener use] instead, to tell your UIWebView to handle clicked URL.

-(void)webView:(WebView *)webView decidePolicyForNavigationAction:(NSDictionary *)actionInformation request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id )listener
{
    [listener use]
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!