11

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")
    }
}
Grantism
  • 374
  • 2
  • 3
  • 11
  • 5
    Did you set webView's delegate? – Shai Jun 19 '14 at 11:43
  • 6
    Yeah, just add `webview.delegate = self;` in your `viewDidLoad`. – Cezary Wojcik Jun 19 '14 at 11:49
  • Thanks heaps.It looks like I hadn't set the delegate for the webview. As a newbie, it's not really made very clear that you have to do that in any of the tutorials I've been going through. I did find this video helpful though: https://www.youtube.com/watch?v=iFF1c-RgMkU – Grantism Jun 19 '14 at 12:12

6 Answers6

20

Try this!

var req = NSURLRequest(URL: url)
webView.delegate = self
webView.loadRequest(req)
LinusGeffarth
  • 27,197
  • 29
  • 120
  • 174
LLIAJLbHOu
  • 1,313
  • 12
  • 17
2

I experienced the same issue, even I did confirmed the UIWebViewDelete to self and implemented its methods.

//Document file url
var docUrl = NSURL(string: "https://www.google.co.in/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&ved=0ahUKEwjjwPSnoKfNAhXFRo8KHf6ACGYQFggbMAA&url=http%3A%2F%2Fwww.snee.com%2Fxml%2Fxslt%2Fsample.doc&usg=AFQjCNGG4FxPqcT8RXiIRHcLTu0yYDErdQ&sig2=ejeAlBgIZG5B6W-tS1VrQA&bvm=bv.124272578,d.c2I&cad=rja")

let req = NSURLRequest(URL: docUrl!)
webView.delegate = self
//here is the sole part
webView.scalesPageToFit = true
webView.contentMode = .ScaleAspectFit
webView.loadRequest(req)

above logic worked perfectly with test URl I got from quick google search.

But I when I replaced with mine. webViewDidFinishLoad never get called.

then How we solved?

On backed side we had to define content-type as document in headers. and it works like charm.

So please make sure on your server back-end side as well.

swiftBoy
  • 35,607
  • 26
  • 136
  • 135
2

Here's my 2 cents battling with the same problem in SWIFT 3:

class HintViewController: UIViewController, UIWebViewDelegate

Declare the delegate methods this way (note the declaration of the arguments):

func webViewDidStartLoad(_ webView: UIWebView)
func webViewDidFinishLoad(_ webView: UIWebView)

Remember to set self to the webview's delegate property either in Interface Builder (Select the webview, drag from the delegate outlet to the webview from the Connections Inspector OR programmatically: self.webview.delegate = self)

1

As others noted, setting the delegate of UIWebView and conforming to the UIWebViewDelegate protocol is the best possible solution out there.

Sergey Grischyov
  • 11,995
  • 20
  • 81
  • 120
1

For other's who might make it here. I had put my delegate methods in a private extension which couldn't be accessed by the delegate caller. Once I changed the extension to internal the delegates started to get called properly.

teradyl
  • 2,584
  • 1
  • 25
  • 34
1

There is my detailed solution for Swift 3:

1) in class declaration write the UIWebViewDelegate. For example:

class MyViewController: UIViewController, UIWebViewDelegate {

2) of course in storyboard make link to your UIViewController like this: @IBOutlet weak var webView: UIWebView!

3) in the func viewDidLoad add one line: self.webView.delegate = self

Nothing more. Special thinks to LinusGeffarth and LLIAJLbHOu for idea.

OrdoDei
  • 1,379
  • 2
  • 16
  • 9