2016-05-12 8 views
0

Я написал этот код, чтобы показать pdf, используя UIDocumentInteractionController. Но я не знаю, как искать pdf в локальном каталоге и открывать в iOS 8 и ниже. Любая помощь?открыть PDF-адрес в iOS 8?

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

Вы можете просмотреть PDF в iOS 8 с помощью webview. Попробуйте ниже кода,

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) 
    } 

ИЛИ

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 not ios 8. –

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 
} 

 Смежные вопросы

  • Нет связанных вопросов^_^