0

I am attempting to load a lunch menu PDF into a web view for a high school app that I am updating. Currently, it can load a PDF into the web view and display it just fine, but I want to speed up the monthly update process by having my app receive the link through Parse (Which I can update much quicker than updating the link in the app itself with Apple's 7 day review period), and then load the PDF. Currently, with what I have put together, my app will not load the PDF. Here's the entire view:

import UIKit

class AlaCarte_ViewController: UIViewController {
@IBOutlet weak var webviewAlaCarte: UIWebView!

var urlpath = String()

func loadAddressUrl(){


    let requestURL = NSURL (string:urlpath)
    let request = NSURLRequest(URL: requestURL!)
    webviewAlaCarte.loadRequest(request)

    alaCarteUpdate()

}

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    clearPDFBackground(self.webviewAlaCarte)



}

func clearPDFBackground(webView: UIWebView) {
    var view :UIView?
    view = webView as UIView

    while view != nil {
        if NSStringFromClass(view?.dynamicType) == "UIWebPDFView" {
            view?.backgroundColor = UIColor.clearColor()

        }

        view = view?.subviews.first as! UIView?

    }
}

func alaCarteUpdate() {
    var query = PFQuery(className: "AlaCarte")
    query.getObjectInBackgroundWithId("rT7MpEFySU") {(AlaCarte: PFObject!, error: NSError!)-> Void in
        if error == nil && AlaCarte != nil {
            println(AlaCarte)
        } else {
            println(error)
        }

        let AlaCarteLink = AlaCarte["webaddress"] as! String

        self.urlpath = AlaCarteLink

    }
}

override func viewDidLoad() {
    super.viewDidLoad()

    loadAddressUrl()


}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
}
*/

}

The link is stored in my Parse app as "webaddress" and does not contain end quotations. Adding them does not help. Any ideas?

John Harding II
  • 564
  • 1
  • 6
  • 20

1 Answers1

0

It looks to me like you're not telling the web view to load the URL once it's retrieved from Parse.

Try adding the following lines after self.urlpath = AlaCarteLink in alaCarteUpdate().

let requestURL = NSURL (string:self.urlpath)
let request = NSURLRequest(URL: requestURL!)
self.webviewAlaCarte.loadRequest(request)

I think it would also be a good idea to add a function that specifically loads a url string into your web view, so you can call it from both inside alaCarteUpdate(), and loadAddressUrl(), and avoid the duplicate 3x lines. I've assumed that you're loading the URL in loadAddressURL() so that you can show a local/cached document while retrieving the latest from Parse.

rickerbh
  • 9,731
  • 1
  • 31
  • 35