2017-05-26 116 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