0

My webview is not showing up. (Just a blank white page) I have a TabBarController and two ViewControllers. One of them containing the webview.

My code:

import Foundation
import UIKit

class HomeViewController: UIViewController, UIWebViewDelegate {

    @IBOutlet var webView: UIWebView!


    var urlpath: String = "http://www.google.de"

    func loadAddressURL(){
        let requesturl = NSURL(string: urlpath)
        let request = NSURLRequest(URL: requesturl!)
        webView.loadRequest(request)
    }

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

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


}

Whats wrong here?

simplesystems
  • 839
  • 2
  • 14
  • 28

3 Answers3

0

First double check the view is laying out correctly. Run the app and nav to the screen. Do Debug > View Debugging > Capture View Hierarchy. Right click the screen and print out the view to see if the frame is correct. If this is ok then implement the delegate methods of UIWebView, specifically webViewDidFinishLoad. This should let you know if there was an issue loading the webpage.

Andrew McKinley
  • 1,137
  • 1
  • 6
  • 11
0

Do you have constraints set up in IB? You might try setting them programmatically to see if that helps.

if let wv = myWebView {
    wv.translatesAutoresizingMaskIntoConstraints = false
    view.addConstraints(
       NSLayoutConstraint.constraintsWithVisualFormat(
      "H:|[webView]|", options: [], metrics: nil, views: ["webView":wv]))
    view.addConstraints(
       NSLayoutConstraint.constraintsWithVisualFormat(
       "V:|[webView]|", options: [], metrics: nil, views: ["webView":wv]))
}
Nancy
  • 406
  • 5
  • 15
0

You need to tell the webView who is its delegate, just binding the outlet won't be enough.

In viewDidLoad add this:

self.webView.delegate = self

Then you need to conform to UIWebViewDelegate (which you are already doing, but I suggest you extend your view controller instead):

extension HomeViewController: UIWebViewDelegate {
    func webViewDidStartLoad(_ webView: UIWebView) {
        print("Loading")
    }

    func webViewDidFinishLoad(_ webView: UIWebView) {
        print("Finished")
    }
}

And last but not least, you need to allow your application to load external URL's by adding a transport security, I suggest you check this post.

Please note I'm using Swift 3, you may need to make little changes.

Community
  • 1
  • 1
Alonso Urbano
  • 2,266
  • 1
  • 19
  • 26