sterling archer
sterling archer

Reputation: 334

Swift 2 - Cannot load UIWebView in separate ViewController

I'm running into a bizarre problem - I cannot load a UIWebView in the background of a separate viewcontroller. Specifically, I have a UITableViewCell with a button to load a UIWebView in the viewcontroller behind it. However, I keep getting the error "found nil while unwrapping an Optional value"...

Here is my code:

func loadWebView(URLString:String) {

    print("the urlstring is \(URLString)")
    let theRequestURL = NSURL (string: URLString)
    let theRequest = NSURLRequest (URL: theRequestURL!)

    fullScreenWebView.loadRequest(theRequest)


}

The print statement yields the correct URL, so the issue isn't that the URL string isn't being passed.

Furthermore, if I load the UIWebView from the viewcontroller it's defined in, it works perfectly. I'm guessing the issue has to do with not being the correct viewcontroller when loadWebView() is called... I read Unable to display URL in WebView from another view controller, which was somewhat helpful.

What am I doing wrong?

Upvotes: 0

Views: 708

Answers (2)

UditS
UditS

Reputation: 1956

When calling loadRequest using

SecondViewController().loadWebView("URL STRING HERE")

You are creating a new instance of the SecondViewController rather than using the one that has already been loaded.

What you need is call loadRequest on the instance that is already in memory, and has your UIWebView initialized.

For this you can get reference of the previous view controller and call a method to load your request

Assuming SecondViewController is the controller with UIWebView

Add this In the top view controller (Controller with your UITableView)

func loadWebView(URLString:String) {

    print("the urlstring is \(URLString)")
    let theRequestURL = NSURL (string: URLString)
    if let theRequest = NSURLRequest (URL: theRequestURL!) {

        //In case you push view controller on a navigation stack
        let vc : SecondViewController = self.backViewController()

        vc.loadWebView(theRequest)
    }
}

//Get previous view controller on navigation stack
func backViewController() -> UIViewController? {
    if let stack = self.navigationController?.viewControllers {
        for(var i=stack.count-1;i>0;--i) {
            if(stack[i] == self) {
                return stack[i-1]
            }
        }
    }
    return nil
}

Now in your SecondViewController, add

func loadWebView(request : NSURLRequest) {

    // Replace customWebView with the reference variable of your `UIWebView`
    customWebView.loadRequest(request)
}

Upvotes: 1

Kyle Redfearn
Kyle Redfearn

Reputation: 2281

The reason you are getting this error is because you are force unwrapping a nil value. I would always suggest unwrapping nullable values using an if let statement:

func loadWebView(URLString:String) {

    print("the urlstring is \(URLString)")
    if let theRequestURL = NSURL (string: URLString) {
        let theRequest = NSURLRequest (URL: theRequestURL!)

        fullScreenWebView.loadRequest(theRequest)
    }
}

Not every string can be converted into a URL, which is why NSURL(string:) returns a optional value. I would inspect why your URLString can't be parsed into a URL

Upvotes: 0

Related Questions