【问题标题】:open PDF URL in iOS 8?在 iOS 8 中打开 PDF URL?
【发布时间】:2026-01-19 15:10:01
【问题描述】:

我已经编写了这段代码来使用 UIDocumentInteractionController 显示 pdf。但是,我不知道如何在本地目录中搜索 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?
                }
            }

【问题讨论】:

    标签: swift ios8 nsfilemanager uidocumentinteraction


    【解决方案1】:

    您可以在 iOS 8 中使用 webview 查看 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)
        }
    

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

    希望这对你有帮助。

    【讨论】:

    • 我不想使用 webview。我想使用 UIDocumentInteraction。有什么帮助吗?
    • 好的,你试过用这个 'presentOpenInMenuFromRect' 吗?参考这个链接:*.com/questions/25430069/…
    • 问题在于语句“let fileURL = NSURL(fileURLWithPath: "(filename)_my_invoice.pdf", isDirectory: false, relativeToURL: pathURL)"。意思是 NSURL,fileURLWithPath 仅在 ios 9 上可用IOS 8.
    • 好的,我编辑了我的答案,所以请检查一下。与 UIDocumentInteraction 一起使用。
    • 我不确定,但你可以参考这个链接:*.com/questions/27627116/…
    【解决方案2】:

    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
    }
    

    【讨论】:

      最近更新 更多