UIWebView crashing on second pdf load request

前端 未结 3 1290
情话喂你
情话喂你 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];
    

提交回复
热议问题