2017-08-03 33 views
1

我想知道是否有快速的方法来检查PDF文件的格式是否正确,并且在将它提供给之前可以通过UIWebView打开UIWebView如何检查PDF是否有效并且可以在UIWebView中打开

如果PDF文件无法打开,我最终得到了这个代表电话,但我想根据是否打开该文件来决定是否将此PDF传递给UIWebView。我尝试了NSFileManager,但无法找到任何可靠的API。任何想法如何实现?

public func webView(_ webView: UIWebView, didFailLoadWithError error: Error) 

请注意:我加载从Documents目录这个PDF。由于数据下载不完整,此PDF可能无效。

+0

如果PDF文件无法打开 - 这意味着您的网址是无效的URL或其他 –

+0

@ Anbu.Karthik - 请参阅我的更新说明。 – Abhinav

+0

看到这一次https://stackoverflow.com/questions/21856923/ios-check-if-webview-is-loaded-with-pdf-content-on-non-pdf-file-extension –

回答

1

可能是因为它是无效的/损坏或别的东西不能打开,你可以试试下面的代码行,并检查它是否是有效的PDF文档,并可以打开:

NSString *path = [[NSBundle mainBundle] pathForResource:@"example_PDF" ofType:@"pdf"];//Path of your PDF 
NSData *data = [[NSFileManager defaultManager] contentsAtPath:path]; 

CGDataProviderRef provider = CGDataProviderCreateWithCFData((__bridge CFDataRef)data); 
CGPDFDocumentRef document = CGPDFDocumentCreateWithProvider(provider); 

if (document == nil) { 
    NSLog(@"The PDF is corrupted");//Can't be opened 
} 
CGDataProviderRelease(provider); 
CGPDFDocumentRelease(document); 
相关问题