UIWebDocumentView _updateSubviewCaches 在 iOS10 中崩溃

UIWebDocumentView _updateSubviewCaches crash in iOS10

我在 iOS10 中遇到更严重的 HockeyApp 崩溃问题。请找到下面给出的崩溃日志。

线程 4 崩溃:

0 libobjc.A.dylib 0x0000000187242f30 objc_msgSend + 16
1 UIKit 0x000000018e86e914 -[UIWebDocumentView _updateSubviewCaches] + 36
2 UIKit 0x000000018e69093c -[UIWebDocumentView subviews] + 88
3 UIKit 0x000000018e941bd4 -[UIView(CALayerDelegate) _wantsReapplicationOfAutoLayoutWithLayoutDirtyOnEntry:] + 68
4 UIKit 0x000000018e63d770 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1248
5 QuartzCore 0x000000018bb0640c -[CALayer layoutSublayers] + 144
6 QuartzCore 0x000000018bafb0e8 CA::Layer::layout_if_needed(CA::Transaction*) + 288
7 QuartzCore 0x000000018bafafa8 CA::Layer::layout_and_display_if_needed(CA::Transaction*) + 28
8 QuartzCore 0x000000018ba77c64 CA::Context::commit_transaction(CA::Transaction*) + 248
9 QuartzCore 0x000000018ba9f0d0 CA::Transaction::commit() + 508
10 QuartzCore 0x000000018ba9faf0 CA::Transaction::observer_callback(__CFRunLoopObserver*, unsigned long, void*) + 116
11 CoreFoundation 0x00000001887a57dc __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 28
12 CoreFoundation 0x00000001887a340c __CFRunLoopDoObservers + 368
13 CoreFoundation 0x00000001886d2068 CFRunLoopRunSpecific + 472
14 WebCore 0x000000018d273a2c RunWebThread(void*) + 452
15 libsystem_pthread.dylib 0x000000018788b860 _pthread_body + 236
16 libsystem_pthread.dylib 0x000000018788b770 _pthread_start + 280
17 libsystem_pthread.dylib 0x0000000187888dbc thread_start + 0

知道这里发生了什么吗? 似乎以下崩溃组也相关。

崩溃组 1

[UIWebBrowserView _collectAdditionalSubviews]
objc_msgSend() selector name: autorelease

崩溃组 2

[UIWebDocumentView subviews]
objc_msgSend() selector name: arrayByAddingObjectsFromArray:

我正在回答我自己的问题,因为我能够重现这个问题并找到根本原因。

请注意以下事项。

1.我正在使用 UIWebView。首先,不推荐这样做。

2。以下实现方式会导致此问题。我在这里写伪代码来演示这个问题。

@implementation MyViewController

    - (void)loadView {

        //creating UIWebView Here
        self.webView = [[UIWebView alloc] initWithFrame:_some_frame];

        //setting the web view properties here.

        //Adding to the view
        [self.view addSubView: self.webView];

        [self populateWebView];
    }

    - (void) populateWebView {
        [self.webView loadHTMLString:_some_html 
                             baseURL:[NSURL fileURLWithPath:_some_path]];
    }


    - (void)viewDidLayoutSubviews {
        [super viewDidLayoutSubviews];

        if (self.webView) {
           //The following two lines causing this issue.
           self.webView.scrollView.contentInset = _some_insets;
           self.webView.scrollView.scrollIndicatorInsets = _some_insets;
        }
    }

@end

3。下面给出解决方法

@implementation MyViewController

    - (void)loadView {

        //creating UIWebView Here
        self.webView = [[UIWebView alloc] initWithFrame:_some_frame];

        //setting the web view properties here.

        //Adding to the view
        [self.view addSubView: self.webView];

        [self populateWebView];
    }

    - (void) populateWebView {
        [self.webView loadHTMLString:_some_html 
                             baseURL:[NSURL fileURLWithPath:_some_path]];
    }


    - (void)viewDidLayoutSubviews {
        [super viewDidLayoutSubviews];
    }

    - (void)webViewDidFinishLoad:(UIWebView *)webView {
        webView.scrollView.contentInset = _some_insets;
        webView.scrollView.scrollIndicatorInsets = _some_insets;
    }

@end

在我的实际代码中,我在这里使用自定义 WebView class,方法是通过 UIWebView 进行子class。不认为这会造成一些问题。根本原因是在 viewDidLayoutSubviews 中设置了“contentInset”和“scrollIndicatorInsets”,这不是一个好主意主意。当我放置断点时,“viewDidLayoutSubviews”调用了几次,最终 App 崩溃了。

作为解决方案,我将以下代码部分移至“webViewDidFinishLoad”,只有当 WebView 完成加载后准备就绪时才会调用。所以在这个委托方法下添加这段代码是有意义的。

webView.scrollView.contentInset = _some_insets;
webView.scrollView.scrollIndicatorInsets = _some_insets;