ios:EXC_BAD_ACCESS 用于 Webview 委托 [英] ios:EXC_BAD_ACCESS for Webview delegate

查看:22
本文介绍了ios:EXC_BAD_ACCESS 用于 Webview 委托的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解决这些 Crashlytics 问题,但我有此崩溃日志

线程:崩溃:com.apple.main-thread0 libobjc.A.dylib 0x34217f46 objc_msgSend + 51 UIKit 0x29a2d5a3 -[UIWebView webView:decidePolicyForNavigationAction:request:frame:decisionListener:] + 1822 核心基金会 0x2630cad4 __invoking___ + 683 CoreFoundation 0x26239645 -[NSInvocation 调用] + 3004 CoreFoundation 0x2623d0c7 -[NSInvocation invokeWithTarget:] + 505 WebKitLegacy 0x326d9261 -[_WebSafeForwarder forwardInvocation:] + 2246 核心基金会 0x2630b62f ___转发___ + 3547 核心基金会 0x2623d008 _CF_forwarding_prep_0 + 248 核心基金会 0x2630cad4 __invoking___ + 689 CoreFoundation 0x26239645 -[NSInvocation 调用] + 30010 WebCore 0x31c02729 HandleDelegateSource(void*) + 10011 核心基金会 0x262cefbf __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 1412 核心基金会 0x262ce461 __CFRunLoopDoSources0 + 36413 核心基金会 0x262cca35 __CFRunLoopRun + 77214 核心基金会 0x2621a3b1 CFRunLoopRunSpecific + 47615 核心基金会 0x2621a1c3 CFRunLoopRunInMode + 10616 图形服务 0x2d801201 GSEventRunModal + 13617 UIKit 0x2988443d UIApplicationMain + 144018 abc 0x0030dcd7 主 (main.m:14)

我可以理解它对 webview 委托的一些回调并且发生了严重的过度,所以为了纠正这个问题,我通过处理委托

[self.webview stopLoading];self.webview.delegate =nil;

在所有课程中,但我可以看到此崩溃.你能告诉我可能出了什么问题以及纠正这个问题的一些方法吗?

解决方案

以下可能是这种情况

  • 用户将看到一个带有 UIWebView 的屏幕
  • UIViewControllerself 设置为delegate 网页开始下载
  • 用户退出屏幕
  • UIViewController 获取解除分配 UIWebView 完成加载并向其委托发送我已完成加载消息

当 webview 对象不再存在时调用其他一些委托方法,即悬空指针效果

1.始终确保在离开视图

之前停止加载 webView移除委托<块引用>

发布一个 UIWebView 的实例之前,你已经为它设置了一个委托,您必须首先将其 delegate 属性设置为 nil.这个可以完成,在你的 dealloc 方法

这是参考

//如果使用 ARC- (void)dealloc {[_webView setDelegate:nil];[_webView 停止加载];}//如果没有使用 ARC- (void)dealloc {[webView setDelegate:nil];[webView 停止加载];[webView 发布];[超级dealloc];}//ARC - 在 iOS6 之前,因为它已被弃用.- (void)viewWillUnload {[webView setDelegate:nil];[webView 停止加载];}

2.确保您在viewWillDisappear中没有将stopLoadingsetDelegate设为nil

<块引用>

如果 ViewController 是另一个 ViewController 的孩子,你可以触发 从父视图中移除 ViewController 的视图带有动画的 ViewController 视图.同时,你可以从其父级中删除 ViewController 并nil 删除它的 reference.此时 ViewController 将是 nilviewWillDisappear永远不会被调用,这意味着WebView 委托永远不会被调用清理

使用 dealloc 并确保您的 WebView 始终被清理.

3.确保将webviewsubviewsContentOffset设置为CGPointZero,无动画<块引用>

在某些版本的 iPad 中,如果关闭父级,则 webview 会滚动没有将 ContentOffset 设置为 CGPointZero 的视图控制器各种问题都会来

所以最好在关闭它之前调用父视图控制器中的以下代码

 for (id subview in webView.subviews){if ([[subview class] isSubclassOfClass: [UIScrollView class]]){[子视图 setContentOffset:CGPointZero 动画:NO];}}

希望这会有所帮助.如有疑问,请随时提出.

4. 一般而言,您不应该在 UIScrollView 对象中embed UIWebView 对象.如果这样做,可能会导致意外行为,因为这两个对象的触摸事件可能会混淆并错误处理.

这是参考

I have a situation where I am trying to resolve these Crashlytics issues and I have this crash log

Thread : Crashed: com.apple.main-thread
0  libobjc.A.dylib                0x34217f46 objc_msgSend + 5
1  UIKit                          0x29a2d5a3 -[UIWebView webView:decidePolicyForNavigationAction:request:frame:decisionListener:] + 182
2  CoreFoundation                 0x2630cad4 __invoking___ + 68
3  CoreFoundation                 0x26239645 -[NSInvocation invoke] + 300
4  CoreFoundation                 0x2623d0c7 -[NSInvocation invokeWithTarget:] + 50
5  WebKitLegacy                   0x326d9261 -[_WebSafeForwarder forwardInvocation:] + 224
6  CoreFoundation                 0x2630b62f ___forwarding___ + 354
7  CoreFoundation                 0x2623d008 _CF_forwarding_prep_0 + 24
8  CoreFoundation                 0x2630cad4 __invoking___ + 68
9  CoreFoundation                 0x26239645 -[NSInvocation invoke] + 300
10 WebCore                        0x31c02729 HandleDelegateSource(void*) + 100
11 CoreFoundation                 0x262cefbf __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 14
12 CoreFoundation                 0x262ce461 __CFRunLoopDoSources0 + 364
13 CoreFoundation                 0x262cca35 __CFRunLoopRun + 772
14 CoreFoundation                 0x2621a3b1 CFRunLoopRunSpecific + 476
15 CoreFoundation                 0x2621a1c3 CFRunLoopRunInMode + 106
16 GraphicsServices               0x2d801201 GSEventRunModal + 136
17 UIKit                          0x2988443d UIApplicationMain + 1440
18 abc                          0x0030dcd7 main (main.m:14)

I can understand that its some callback on webview delegate and bad excess has occurred, so to rectify this I handled the delegates via

[self.webview stopLoading];
self.webview.delegate =nil;

in all the classes, yet I can see this crash. Can you enlighten me what's possibly going wrong and some approach to rectify this?

解决方案

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

这篇关于ios:EXC_BAD_ACCESS 用于 Webview 委托的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆