user3450380
user3450380

Reputation: 1

UIWebView not loading web page

I've been trying to learn iOS development with Swift but I've been stuck trying to load a simple web page for a week or so. I've looked all around and the following code seems to work for others but when I run the iOS simulator nothing shows up but the background for the UIWebView.

class ViewController: UIViewController {

   @IBOutlet weak var myWebView: UIWebView!

   let myURL = NSURL(fileURLWithPath: "https://www.google.com")

   override func viewDidLoad() {
      super.viewDidLoad()
      // Do any additional setup after loading the view, typically from a nib.

      myWebView.loadRequest(NSURLRequest(URL: myURL!))
   }

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

Upvotes: 0

Views: 513

Answers (1)

Chris Loonam
Chris Loonam

Reputation: 5745

You should be using this

let myURL = NSURL(string: "https://www.google.com")

not fileURLWithPath:.

The issue is that, by using fileURLWithPath:, the web view searches the filesystem for a file named "https://www.google.com" and doesn't find one, as no such file exists.

Upvotes: 4

Related Questions