我想知道是否有一种快速的方法来检查 PDF 文件的格式是否正确,并且可以在将其提供给 UIWebView
之前通过 UIWebView
打开>?
如果无法打开 PDF 文件,我最终会收到此委托(delegate)回调,但我想根据是否打开此 PDF 来决定将此 PDF 传递给 UIWebView
。我尝试使用 NSFileManager
但找不到任何可靠的 API。知道如何实现吗?
public func webView(_ webView: UIWebView, didFailLoadWithError error: Error)
请注意:我正在从 Documents 目录加载此 PDF。由于数据下载不完整,此 PDF 可能无效。
最佳答案
可能是因为无效/损坏或其他原因无法打开,您可以尝试以下代码行并检查它是否是有效的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);
关于ios - 如何检查 PDF 是否有效并且可以在 UIWebView 中打开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45477017/