3

Really cannot figure this one out, the URL prints and is not equal to nil, and it works in the browser when I paste it in. Any ideas?

import UIKit

class WebViewController: UIViewController {

    var postLink: String = String()
    @IBOutlet weak var mywebView: UIWebView!

    override func viewDidLoad() {
        super.viewDidLoad()
        print(postLink)

        let attempt = postLink
        let url: URL = URL(string: attempt)!
        let request: URLRequest = URLRequest(url: url)
        mywebView.loadRequest(request)

    }

The error occurs at:

let url: URL = URL(string: attempt)!

Noah
  • 101
  • 1
  • 2
  • 7
  • 1
    Why don't you supply the URL? What's `let attempt = postLink` doing? When do you get when you try the 4 lines of load code in `viewWillAppear`? What's the value of `url` after assignment? Implement `UIWebViewDelegate` and see how/if things progress or fail. – meaning-matters Jul 28 '17 at 19:51
  • what happened was the url has some extra white space and lines at the end, thank you for the help. – Noah Jul 30 '17 at 05:30

2 Answers2

2

I am guess you are passing the urlString from another controller, do that instead

var postUrlString:String? //<-- make it optional 

override func viewDidLoad() {
   super.viewDidLoad()

   guard let urlString = postUrlString, // forced unwrapped 
         let url = URL(string: urlString)
        else {  return }  // if there is any optional we return 
 // else continue
        let request: URLRequest = URLRequest(url: url)
        mywebView.loadRequest(request)

}
Lamour
  • 3,002
  • 2
  • 16
  • 28
  • // if there is any optional we return, what is meant by this? – Mohammad Sadiq Jul 28 '17 at 20:10
  • if something is `nil` in this case postUrlString or url @MohammadSadiq – Lamour Jul 28 '17 at 20:21
  • so please change it to if something is nil. Because its optional for sure, what we are checking is, whether its nil or not – Mohammad Sadiq Jul 28 '17 at 20:24
  • that is exactly what the code does lol, first line is we try to extract urlString from an optional, if we succeed then create url if not don't execute the rest of the code – Lamour Jul 28 '17 at 20:26
  • I used the guard method and it works, it is no longer nil but the webview will not load the request? the postLink prints just fine as well – Noah Jul 28 '17 at 21:57
2

The error is simple, postLink, you are providing to create URL is not correct. My guess is its empty.(Just a guess) and you have forgot to set it.

Avoid using force unwrapping ! in your code as much as possible. You should either use guard let or if let in the scenarios. In your case you might want to show some error to user when you are unable to load. Instead of

let url: URL = URL(string: attempt)!

use

    if let url = URL(string: attempt) {
        let request = URLRequest(url: url)
        mywebView.loadRequest(request)
    } else {
// Do something like. Show an alert that could not load webpage etc. 
}

Alternatively you can use guard let, but it would require to return from the function where it is used. To know more about uses of if and guard let you can go through by blog post here.

Mohammad Sadiq
  • 5,070
  • 28
  • 29
  • Had success with the guard method but now cannot get the webview to load any suggestions? – Noah Jul 28 '17 at 21:57