2017-05-26 44 views
0

UIDocumentInteractionController不适用于具有多个页面的大型PDF文件。Swift:UIDocumentInteractionController不工作?

在我的代码

这里,

 var docController:UIDocumentInteractionController! 

...

DispatchQueue.main.async (execute: { [weak self] in 

      self?.docController = UIDocumentInteractionController(url: pdfFileURL!) 
      self?.docController.delegate = self 
      self?.docController.name = pdfFileURL?.lastPathComponent 
      self?.docController.presentPreview(animated: true) 
    }) 

和委托方法,

func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController { 
    return self 
} 

这是在操作台的警告,

2017-05-26 12:46:51.178894 MyApp [3350:1136818] [default] View service did terminate with error: Error Domain=_UIViewServiceInterfaceErrorDomain Code=3 "(null)" UserInfo={Message=Service Connection Interrupted} #Remote

下面连接的是空白图像,

enter image description here

请帮我谢谢。

+0

任何运气?我在一艘类似的船上。我有一切设置正确,但是当我调用presentPreview(animated:)时,它每次都返回false。它也没有调用我的委托方法'documentInteractionControllerViewControllerForPreview' – atreat

回答

0

(1)此问题通常发生在模拟器中。在真实设备上检查它。

如果情况并非如此

(2)试试这个,

class ViewController: UIViewController, UIDocumentInteractionControllerDelegate { 

    var docController:UIDocumentInteractionController! 


    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 

     if let fileURL = NSBundle.mainBundle().pathForResource("SamplePDF1", ofType: "pdf") { // Use if let to unwrap to fileURL variable if file exists 
      docController = UIDocumentInteractionController(URL: NSURL(fileURLWithPath: fileURL)) 

      docController.name = NSURL(fileURLWithPath: fileURL).lastPathComponent 

      docController.delegate = self 

      docController.presentPreviewAnimated(true) 
     } 


    } 

    func documentInteractionControllerViewControllerForPreview(controller: UIDocumentInteractionController) -> UIViewController { 
     return self 
    } 
    func documentInteractionControllerDidEndPreview(controller: UIDocumentInteractionController) { 
     docController = nil 
    } 
+0

感谢您的回复@Maddy。 iPad和iPhone都会出现此问题。其实,上面的屏幕截图是从我的iPhone。 – Raju

+0

然后它可能是内存问题。 – Maddy

+0

那么,我们该如何解决这个问题呢?有什么建议或需要尝试从github的任何框架。 – Raju

0

尝试这样的:

声明:本var interaction: UIDocumentInteractionController?

然后加入

interaction = UIDocumentInteractionController(url: URL(string: "<PDF FILE PATH>")!) 
interaction.delegate = self 
interaction.presentPreview(animated: true) // IF SHOW DIRECT 

,或者如果需要任何建议弹出

interaction.presentOpenInMenu(from: /*<SOURCE BUTTON FRAME>*/, in: self.view, animated: true) 

实现委托 - 与此>UIDocumentInteractionControllerDelegate

public func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController { 
    return self 
} 

public func documentInteractionControllerDidEndPreview(_ controller: UIDocumentInteractionController) { 
    interaction = nil 
} 
+0

'UIDocumentInteractionController'应该初始化使用VAR使其变得可变 – Maddy

+0

我更新了我的答案 –

+0

嗨@AbhishekThapliyal感谢您的答复。我尝试了上面的代码,但它不工作。 – Raju