UIWebView crashing on second pdf load request

前端 未结 3 1292
情话喂你
情话喂你 2021-02-05 11:58

I have a UIWebView I\'m using to show several small PDF\'s. The user selects a news article from a table and then the article(PDF) is loaded in a UIWebView on the same screen.

相关标签:
3条回答
  • 2021-02-05 12:13

    I had a similar issue, with my UIWebView hanging (just freezing at a gray screen with the spinning "loading" circle that never ended). If I exited the program, it would make it crash the second time I loaded.

    Finally I did enough tracing into it and flailing around that I found that if I loaded a label font (with a CCLabelTTF using Cocos2d) from the app resource bundle first, THEN tried to load the PDF, then it would work and wouldn't crash.

    It's a very ugly hack, but someone else might find it useful.

    // HACK: Loading a TTF font first is needed to properly load PDF / HTML
    CCLabelTTF* label = [CCLabelTTF labelWithString:@"Testing" fontName:@"DIN-Black" fontSize:12];
    [label setVisible:false];
    
    // We never actually use the label -- we just create it so that it loads the font
    NSString *path = [[NSBundle mainBundle]
                      pathForResource:myPDF
                      ofType:@"pdf"];
    
    CCLOG(@"Retrieving PDF from '%@'", path);
    
    NSURL *pdfUrl = [NSURL fileURLWithPath:path];
    NSURLRequest *request = [NSURLRequest requestWithURL:pdfUrl];
    
    [webView loadRequest:request];
    [webView setScalesPageToFit:YES];
    [super onEnter];
    
    0 讨论(0)
  • 2021-02-05 12:23

    I'm seeing something similar - both while freeing a webview after loading a PDF or while loading html after loading a PDF. It appears to be rdar://10431759 (see http://openradar.appspot.com/10431759 ). I don't have a way to work around it. I can reproduce it by loading a pdf and then loading the html string @"<div></div>", so it doesn't appear to be a delegate issue.

    If you type "bt" into the gdb console, you may get the real stacktrace, including:

    #0  0x37ccd1c8 in objc_exception_throw ()
    #1  0x381817b8 in +[NSException raise:format:arguments:] ()
    #2  0x381817b8 in +[NSException raise:format:arguments:] ()
    #3  0x381817da in +[NSException raise:format:] ()
    #4  0x35462628 in -[NSObject(NSKeyValueObserverRegistration) _removeObserver:forProperty:] ()
    #5  0x35462296 in -[NSObject(NSKeyValueObserverRegistration) removeObserver:forKeyPath:] ()
    #6  0x31fc3448 in -[UIWebPDFView _removeBackgroundImageObserverIfNeeded:] ()
    #7  0x31fc36a8 in -[UIWebPDFView dealloc] ()
    #8  0x37cc90c4 in _objc_rootRelease ()
    #9  0x31dd2614 in -[UIWebPDFViewHandler clearAllViews] ()
    #10 0x31d76708 in -[UIWebPDFViewHandler _replacePDFViewIfPresentWithWebDocView:] ()
    #11 0x31d766a6 in -[UIWebPDFViewHandler _removePDFViewIfWebDocViewIsNotPDF:] ()
    #12 0x31d76644 in -[UIWebBrowserView webView:didFirstVisuallyNonEmptyLayoutInFrame:] ()
    

    When debugging on the device, you can type "frame 0" and "po $r0" to see the exception message: (I think it's "po $eax" in the simulator.)

    (gdb) frame 0
    #0  0x37ccd1c8 in objc_exception_throw ()
    (gdb) po $r0
    Cannot remove an observer <UIWebPDFView 0x4a3200> for the key path "backgroundImage" from <UIPDFPageView 0x4a4bf0> because it is not registered as an observer.
    

    Edit: This problem only seems to occur when I have "Break on throw" turned on for Objective-C exceptions.

    0 讨论(0)
  • 2021-02-05 12:25

    I have this exact problem as well. Big thanks to Steve for helping me narrow it down even further, my exception is the same as his.

    Do you have break on all exceptions set? I found that if I disable that breakpoint, it doesn't crash any more. Which got me thinking it was just some bug in the new debugger or iOS version? The other thing that got me thinking that was that this crash does not occur when I run on a device with iOS 4.3.x or the 4.3 simulator.

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