2016-05-12 4 views
0

私はUIDocumentInteractionController.Butを使ってpdfを表示するためにこのコードを書いていますが、ローカルディレクトリでpdfを検索し、iOS 8以降で開く方法はわかりません。iOS 8でPDFのURLを開きますか?

let filename = history.invoiceLongDate // 01223642 
      if !filename.isEmpty{ 
       let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) 
       let docs = paths[0] 
       let pathURL = NSURL(fileURLWithPath: docs, isDirectory: true) 
       if #available(iOS 9.0, *) { 
        let fileURL = NSURL(fileURLWithPath: "\(filename)_my_invoice.pdf", isDirectory: false, relativeToURL: pathURL) 
        self.docController = UIDocumentInteractionController(URL: fileURL) 
        self.docController?.delegate = self 
        self.docController?.presentOptionsMenuFromRect(sender.frame, inView: self.view, animated: true) 
       } else { 
        // Fallback on earlier versions 
        // Any Help with that? 
       } 
      } 

答えて

0

webviewを使用してiOS 8でPDFを表示できます。コードの下に試してみてください、

if let pdf = NSBundle.mainBundle().URLForResource("myPDF", withExtension: "pdf", subdirectory: nil, localization: nil) { 
     let req = NSURLRequest(URL: pdf) 
     let webView = UIWebView(frame: CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height)) 
     webView.loadRequest(req) 
     self.view.addSubview(webView) 
    } 

OR

if let baseUrl = NSURL.fileURLWithPath(pathURL) { 
let fileURL = baseUrl.URLByAppendingPathComponent(NFConstants.NFCoreDataStringIdentifiers.CoreDataStoresPathComponent.rawValue) 
} 

が、これはあなたに役立つことを願っています。

+0

私はwebview.IがUIDocumentInteraction.Anyヘルプを使用する場合に使用したくありませんか? –

+0

さて、この 'presentOpenInMenuFromRect'を使って試してみましたか?このリンクを参照してください:http://stackoverflow.com/questions/25430069/uidocumentinteractioncontroller-displaying-blank-pdf –

+0

問題は、 "let fileURL = NSURL(fileURLWithPath:" \(filename)_my_invoice.pdf "、isDirectory:false 、relativeToURL:pathURL) "NSURLを意味し、fileURLWithPathはios 9ではなくios 9でのみ利用可能です。 –

0

UIDocumentInteractionControllerは(iOS 3.2、*)で利用できます。 PDFファイルを閲覧するための

var documentInteractionController: UIDocumentInteractionController! 

@IBAction func openDocument(sender: UIButton) { 
    let URL: NSURL = NSBundle.mainBundle().URLForResource("pdf-sample", withExtension: "pdf")! 

    if (URL != "") { 
     // Initialize Document Interaction Controller 
     self.documentInteractionController = UIDocumentInteractionController(URL: URL) 

     // Configure Document Interaction Controller 
     self.documentInteractionController.delegate = self 

     // Present Open In Menu 
     self.documentInteractionController.presentOptionsMenuFromRect(sender.frame, inView: self.view, animated: true) 
      //presentOpenInMenuFromRect(button.frame, inView: self.view, animated: true) 
    } 
} 

// MARK: UIDocumentInteractionControllerDelegate 
func documentInteractionControllerViewControllerForPreview(controller: UIDocumentInteractionController) -> UIViewController { 
    return self 
} 
関連する問題