Check if UIWebview is displaying PDF

别来无恙 提交于 2019-12-21 20:05:56

问题


To be clear, my question is not asking how to display a PDF in a UIWebview. I want to check if the user has navigated to a PDF (.pdf) document from some website or link. That way I can show more options to the user, like saving the PDF or printing it. What's the best way to check if the UIWebView's content is a PDF?


回答1:


You can check the MIME type too, for a slightly longer journey:

@property (nonatomic, strong) NSString *mime;

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{
   mime = [response MIMEType];
}

- (BOOL)isDisplayingPDF {
    NSString *extension = [[mime substringFromIndex:([mime length] - 3)] lowercaseString];

    return ([[[self.webView.request.URL pathExtension] lowercaseString] isEqualToString:@"pdf"] || [extension isEqualToString:@"pdf"]);
}



回答2:


It's a long trip, but I think this will work ...

BOOL isPdf = [[[self.webView.request.URL pathExtension] lowercaseString]
                 isEqualToString:@"pdf"];


来源:https://stackoverflow.com/questions/14742149/check-if-uiwebview-is-displaying-pdf

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