ios11 UIDocumentInteractionController presentPreviewAnimated会冻结应用程序

时间:2018-01-15 13:17:45

标签: pdf ios11 freeze preview uidocumentinteractioncontroller

当我想用此代码显示PDF文件的预览时,iOS11上的iPad应用程序出现问题:

self.documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:urlPathToDocument]];
[self.documentInteractionController setDelegate:self];
[self.documentInteractionController presentPreviewAnimated:YES];

此应用程序冻结,我必须杀死该应用程序,她不会崩溃,并且不会显示错误。

在iOS10上,没问题,PDF文件的预览显示完好而不冻结应用程序。

我尝试将此代码放在dispatch_async bloc中,但没有更改。

你可以帮帮我吗? 谢谢。

2 个答案:

答案 0 :(得分:1)

添加我自己的答案,因为我遇到了类似的问题,我的搜索解决方案出现了这个问题。我遇到的问题是pdf会在模拟器中打开,但是当我尝试在设备上打开时,它只会显示一个带有pdf标题的空白pdf页面。我收到一条错误,上面写着"无法为路径发出文件扩展名。"

然而,这是我发现的:

直接从捆绑包生成我的网址适用于模拟器但不适用于设备

// This was not working on device, but did work with simulator  
let fileURL = Bundle.main.url(forResource: "SomePDF", withExtension: "pdf")!
self.docVC = UIDocumentInteractionController(url: fileURL)
self.docVC.delegate = self
self.docVC.presentPreview(animated: true)

所以只是为了看看会发生什么我拿了相同的url,将其转换为数据并使用新的url保存到临时目录。最终工作:

// Now it opens correctly on both simulator and device 
let url = Bundle.main.url(forResource: "SomePDF", withExtension: "pdf")!
let pdfData = try! Data(contentsOf: url)
let temp = NSTemporaryDirectory()
let fileURL = URL(fileURLWithPath: temp).appendingPathComponent("SomePDF.pdf")
try! pdfData.write(to: fileURL)
self.docVC = UIDocumentInteractionController(url: fileURL)
self.docVC.delegate = self
self.docVC.presentPreview(animated: true)

希望这有助于其他任何人遇到此问题。使用的是iPhone X iOS 11.2.1

答案 1 :(得分:0)

这似乎是我的iPad上的一个错误,重启2次后,问题似乎得到解决