Search code examples
iosswiftparse-platformuiwebviewxcode7

View Parse PFFile PDF in WebView using Xcode7 and Swift 2


I have an iOS project I'm working on using Xcode7 and Swift2. I have a PDF that is saving to Parse. It is saving to a Parse Class called IncomingRecipe. In this Class is a FileName column with type as a String. It also has a column called PDFData and is type PFFile. I want it so when the user clicks on a TableViewCell it segues to a new View Controller and displays the PDF in a WebView.

Currently these fileNames are in a TableView. I have a segue that goes to the View Controller with the WebView. It passes along the name of the fileName from the TableViewCell as a Global variable.

My query for the data for the TableView code for parse is:

var fileName = [String]()
var PDFData = [PFFile]()

var getRecipeQuery = PFQuery(className: "IncomingRecipe")

// Match the query with only items that the current user uploaded
getRecipeQuery.whereKey("userId", equalTo: appUserId)
getRecipeQuery.findObjectsInBackgroundWithBlock { (objects, error) -> Void in

    // Check to see if 'objects' exist
    if let objects = objects {

        for object in objects {

            // An AnyObject that needs to be cast as a String
            var recipeName = object["fileName"] as! String

            self.fileName.append(object["fileName"] as! String)

            self.objectid.append(object["objectid"] as! String)

            self.userId.append(object["userId"] as! String)

            self.PDFData.append(object["PDFData"] as! PFFile)

            self.myFilesTable.reloadData()

        }

    }

}

The TableView loads the fileName as:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cellIdentifier = "PDFTableViewCell"

    let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! PDFTableViewCell

    cell.textLabel?.text = fileName[indexPath.row]

    return cell
}

I have the code for passing the selected cell fileName to a Global variable as:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if segue.identifier == "ItemView" {

        let savedRecipedView = segue.destinationViewController as! PDFItemViewController

        if let selectedRecipeCell = sender as? PDFTableViewCell {

            let indexPath = myFilesTable.indexPathForCell(selectedRecipeCell)!

            viewingPDFRecipe = fileName[indexPath.row]

            print("Sending Click: \(viewingPDFRecipe)")

        }

    }

}

How can I get the PFFile of the PDA and display it in the WebView on the other View Controller? I can't figure out how to get all of this into a URL to be used with the WebView. I looked here and tried to implement this with mine, with no success. Thank you.


Solution

  • Don't just save the file name with

    self.fileName.append(object["fileName"] as! String)
    

    save the whole list of PFObjects. These objects will contain the file name (that you can use for the table view) and the PFFile reference (that you can use on drill down). Also, you don't appear to, but you shouldn't pass by global. It just looks like you're passing the file name in the segue.

    Instead of passing the file name you should pass the whole PFObject. Then, in the destination view controller you can extract the PFFile reference and the URL it contains.