Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIWebView webViewDidLoadFinish method not called

Tags:

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")
    }
}
like image 684
Grantism Avatar asked Jun 19 '14 11:06

Grantism


2 Answers

Try this!

var req = NSURLRequest(URL: url)
webView.delegate = self
webView.loadRequest(req)
like image 98
LLIAJLbHOu Avatar answered Oct 11 '22 16:10

LLIAJLbHOu


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.

like image 22
swiftBoy Avatar answered Oct 11 '22 17:10

swiftBoy