3

(Swift, iOS8, Xcode6, iPhone/iPad)

webViewDidFinishLoad is not being called, is not firing, and is not working.

Yes, I have set the containing view controller as the delegate. I CTRL-mousedowned on the UIWebView, dragged up to the little yellow circle representing the view controller, and released. A right-click on the UIWebView object shows that the delegate is set.

Yes, I did implement UIWebViewDelegate in my class declaration, like so:

class Paragraph: UIViewController, UIWebViewDelegate {

Yes, I did restart Xcode, and test on both the simulator and an actual iPhone 4S.

The request looks like this:

@IBOutlet var paragraph : UIWebView = nil

var r = NSBundle.mainBundle().pathForResource("cheddar", ofType: "htm")
var u = NSURL(fileURLWithPath: r)
paragraph.loadRequest(NSURLRequest(URL: u))

The callback function looks like this:

func webViewDidFinishLoad() {
    println("webViewDidFinishLoad")
}
kmiklas
  • 13,085
  • 22
  • 67
  • 103
  • `Method does not override any method from its superclass` – kmiklas Jul 10 '14 at 17:50
  • 3
    stupid question, but do you have `paragraph.delegate = self` in there before the `paragraph.loadRequest(…)` call? – Jiaaro Jul 10 '14 at 18:23
  • I put it in there (although I don't see why it's needed if I wired the UIWebView delegate to the containing view controller in storyboard) but `webViewDidFinishLoad` is still not being called. – kmiklas Jul 10 '14 at 18:36
  • Thank you @Jiaaro, this is what I was missing! – Sebastian Grant Jul 19 '16 at 15:00

1 Answers1

4

I got it. The callback was missing a parameter. For posterity:

func webViewDidFinishLoad(webView: UIWebView!) {

Note the webView: UIWebView! parameter

In this case, perhaps even more important, is the way I found the bug. I created an entirely new view controller, and pieced it back together, carefully checking at each step to make sure that I didn't miss anything.

When the Intellisense popup showed the function with the parameter, I saw my error.

NOTE: In Swift 2.2, the UIWebViewDelegate protocol specifies a different optionality: webView: UIWebView. webView: UIWebView! spawns a warning.

Mike Fahy
  • 5,487
  • 4
  • 24
  • 28
kmiklas
  • 13,085
  • 22
  • 67
  • 103