UIWebViewDelegate: webViewDidFinishLoad not called during in-page navigation

我与影子孤独终老i 提交于 2019-12-03 03:33:53

You can try to use NSURLConnectionDataDelegate, it allows you to handle incoming data. Maybe you can determine if the page is loaded manually by adding a sign to your html files.

NSURLConnectionDataDelegate Reference

Edit: gist.github.com/buranmert/7304047 I wrote a piece of code and it worked, that may not be the way you wanted it to work but maybe it helps. Every time user clicks a URL with anchor, it creates another connection and as connection finishes loading web view loads the data, that marks the point where web view finished loading the page. As you use only local html files, I do not think creating connections will create problems

Holly

What you are describing is intended behavior. Just as AJAX or resource requests are never passed to the delegate, only root page changes will ever hit webViewDidFinishLoad:. But I have good news, and it doesn't involve saving a bunch of money on car insurance.

Loads performed within an iFrame DO trigger the full delegate methods and this gives you a solution. You can use this mechanism to post a notification to the native code, just as is often done for console.log() as described in this post. Alternatively, Native Bridge would work well to call into your Objective C code from JavaScript.

Just check weather u got the delegate function DidStartLoading if it is called no doubt that DidFinish also should get called

Are you sure your shouldStartLoadWithRequest always returns an YES???

I always add

return YES;

at the end of shouldStartLoadWithRequest implementation.And that works for me. By returning YES, it denotes that the webview has loaded and would call the webViewDidFinishLoad

nicael
if([webView isLoading]==1)
{
    //your webview is loading
}
else
{
   //your webview has loaded
}

Here is a Swift Implementation of Mert Buran's code incase anybody is looking for it. (Although NSURLConnection is deprecated as of iOS 9)

But it does not solve my problem. When i click on a jquery link that popups a video, it does not fire the webViewDidFinishLoad.

class WebViewController: UIViewController, UIWebViewDelegate, NSURLConnectionDelegate {

var menuURL: String?

var response: NSURLResponse?
var data = NSData()

// MARK: Properties
@IBOutlet weak var webView: UIWebView!

override func viewDidLoad() {
    super.viewDidLoad()

    webView.delegate = self

    // From Web
    let url = NSURL (string: menuURL!)
    let urlRequest = NSURLRequest(URL: url!)

    let connection = NSURLConnection(request: urlRequest, delegate: self)
    self.data = NSData()
    connection!.start()

    // if this is false, page will be 'zoomed in' to normal size
    webView.scalesPageToFit = false

}

func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
    if navigationType == .LinkClicked && request.URL!.fragment != nil {
        let connection = NSURLConnection(request: request, delegate: self)
        connection!.start()
        return false
    }
    return true
}

func connection(connection: NSURLConnection, didReceiveResponse response: NSURLResponse) {
    self.response = response
}

func connection(connection: NSURLConnection, didReceiveData data: NSData) {
    let oldData = NSMutableData(data: self.data)
    oldData.appendData(data)
    self.data = oldData
}

func connectionDidFinishLoading(connection: NSURLConnection) {
    self.webView.loadData(self.data, MIMEType: self.response!.MIMEType!, textEncodingName: "utf-8", baseURL: self.response!.URL!)
    self.data = NSData()
}


}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!