Search code examples
iosuiwebviewswift

UIWebView webViewDidLoadFinish method not called


I've been playing around with web views in swift this evening, but have run into a bit of an issue.

For some reason I'm not able to get the webViewDidStartLoad or webViewDidFinishLoad methods to fire. In my storyboard, I have an outlet called webView linked to my UIWebView element.

Is anyone able to help me with what I am doing wrong?

This is my viewController.swift file:

import UIKit

class ViewController: UIViewController, UIWebViewDelegate {

    @IBOutlet var webView : UIWebView

    var url = NSURL(string: "http://google.com")

    override func viewDidLoad() {
        super.viewDidLoad()

        //load initial URL
        var req = NSURLRequest(URL : url)
        webView.loadRequest(req)
    }

    func webViewDidStartLoad(webView : UIWebView) {
        //UIApplication.sharedApplication().networkActivityIndicatorVisible = true
        println("AA")
    }

    func webViewDidFinishLoad(webView : UIWebView) {
        //UIApplication.sharedApplication().networkActivityIndicatorVisible = false
        println("BB")
    }
}

Solution

  • Try this!

    var req = NSURLRequest(URL: url)
    webView.delegate = self
    webView.loadRequest(req)