简体   繁体   中英

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.
   }
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM