App PDF Viewer Ios in Xcode 10

0

Well, my problem is as follows. I have the entire app made with two Viewcontroller. The problem is that the whole app is developing well, except when displaying the PDF cell in the second screen that does not appear at all. What can I miss? This is the code of the second ViewController where I have a web view from the first, and linked by a Segue: import UIKit import WebKit

class ViewController2: UIViewController, WKUIDelegate {

@IBOutlet var VistaWebPdf: WKWebView!

var nombrePdfRecibido: String?

var vistaWebPdf: WKWebView!

override func loadView() {
    let webConfiguration = WKWebViewConfiguration()
    VistaWebPdf = WKWebView(frame: .zero, configuration: webConfiguration)
    VistaWebPdf.uiDelegate = self
    view = VistaWebPdf

    var vistaWebPdf: WKWebView!

    var nombrePdfRecibido:String?
    func viewDidLoad() {
    super.viewDidLoad()

        mostrarPdf()

}

    }

    func mostrarPdf(){

//1 Dirección del archivo PDF
    let nombrePdfRecibido = "PDF"
    let direccionPdf = URL(fileURLWithPath: Bundle.main.path(forResource: nombrePdfRecibido, ofType: "Pdf", inDirectory: "PDF")!)

// 2 Transform PDF file to Data

    let datosPdf = try? Data(contentsOf: direccionPdf )

// 3 Display Data in the Web View

VistaWebPdf.load (dataPdf !, mimeType: "application / pdf", characterEncodingName: "utf-8", baseURL: direccionPdf)

 }

}

AFTER, I PUT THE CODE OF VIEWCONTROLLER 1 together with the Navigation Controller: //

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

var contenidoCeldas = ["PDF1","PDF2","PDF3","PDF4","PDF5","PDF6","PDF7","PDF8"]
@IBOutlet var tabla: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()

    tabla.dataSource = self
    tabla.delegate = self

}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return contenidoCeldas.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    print(indexPath.section)

    let celda = UITableViewCell(style: UITableViewCell.CellStyle.default, reuseIdentifier:"CellPDF")
    celda.textLabel?.text = contenidoCeldas[indexPath.row]

    return celda
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    _ = indexPath.row
    self.performSegue(withIdentifier: "Pantalla_2Segue"){

    }
    func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "Pantalla_2Segue" {

        let iPdfSeleccionadoRecibido = sender as! NSIndexPath

        let idx = iPdfSeleccionadoRecibido.row
        func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
            if segue.identifier == "Pantalla_2Segue" {
                let idPdfSeleccionadoRecibido = sender as! NSIndexPath
                let idx = idPdfSeleccionadoRecibido.row

                let objPantalla2:ViewController2 = segue.destination as! ViewController2 


    objPantalla2.nombrePdfRecibido = contenidoCeldas[idx]
            }


    }


}
    
asked by user102623 21.10.2018 в 19:11
source

1 answer

0

I would recommend you to use PDFKit, it is a native library of ios very easy to use introduced by Apple in 2017 (watch? v = V-oy8diDApY)

In your first view, execute the segue in the main thread of your program

First view

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        DispatchQueue.main.async(execute: {
            self.performSegue(withIdentifier: "Pantalla_2Segue", sender: nil)
        })
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == self.mySegue {
            let destination:SegundoView = segue.destination as! SegundoView
            destination.urlDePdf = "url_de_pdf"
        }
}

Second view

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = self.tableView.dequeueReusableCell(withIdentifier: cell_id, for: indexPath) as! MiCustomCell

        let pdfView = PDFView()
        pdfView.frame = cell.frame
        cell.addSubview(pdfView)
        if let document = PDFDocument(url: URL(string: urlDePdf) ?? URL(string:"")!) {
                pdfView.displayMode = .singlePageContinuous
                pdfView.autoScales = true
                pdfView.document = document
        }

        return cell

}
    
answered by 14.12.2018 в 23:40