iOS - Check if webView is loaded with PDF content

2019-08-10 21:40发布

I would like to Check (boolean value) if PDF content is loaded in the UIWebView. This question already exists, but not this case.

It's not as simple as just checking the path extension of the webView Url because this PDF content is generated via ASPX. The extension will return 'aspx'. Also not just looking in the url, the string 'pdf' will always be found in the url since I requesting a PDF file. I need to check if the server really returns a PDF file as it should.

Ok, The webView is loaded with PDF content but not to a specific pdf file on the server. (maybe apache redirect,aspx pdf generation or just something else)

If this url is loaded in the Safari Application (to the aspx file), it will detect this PDF content (even if aspx file). It says "open in..." and so on.

How to detect if the UIWebView is loaded with PDF content on non-pdf extention file?

Jonathan

1条回答
Ridiculous、
2楼-- · 2019-08-10 22:23

I solved it using this:

Thanks @Guilherme

-(bool)isPDFContentLoadedInWebView:(UIWebView *)webView {
    NSString *mime =[[NSURLCache sharedURLCache] cachedResponseForRequest:webView.request].response.MIMEType;
    if (!([mime rangeOfString:@"pdf"].location == 0)) {
    return YES;
    } else {
        return NO;
    }

    return NO;
}
查看更多
登录 后发表回答