UIWebDocumentView _updateSubviewCaches crash in iOS10

情到浓时终转凉″ 提交于 2019-12-03 00:43:42
arango_86

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;
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!