UIWebDocumentView _updateSubviewCaches crash in iOS10

前端 未结 1 1905
长情又很酷
长情又很酷 2021-02-03 15:55

I am getting the following crash in HockeyApp more seriously in iOS10. Please find the crash log as given below.

Thread 4 Crashed:

0 libobjc.A.dylib 0x00         


        
1条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-03 16:24

    I am answering to my own question, since I am able to replicate this issue and found the root cause.

    Please note the following.

    1. I am using a UIWebView. First of all, this is not recommended.

    2. The following way of implementation causes this issue. I am writing pseudo-code here to demonstrate the problem.

    @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. The solution is given below.

    @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
    

    In my actual code, I was using a custom WebView class here by subclassing the UIWebView. Not think that will create some issue. The root cause is setting the "contentInset" and "scrollIndicatorInsets" in viewDidLayoutSubviews which is not a good idea. When I put the break points, "viewDidLayoutSubviews" called several times and eventually App crashes.

    As a solution I moved the following part of the code into "webViewDidFinishLoad" which will call only when the WebView is ready after finished loading. So it makes sense to add this code under this delegate method.

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

    0 讨论(0)
提交回复
热议问题