检测 WKWebView 何时完成加载 [英] Detect when WKWebView is finished loading

查看:63
本文介绍了检测 WKWebView 何时完成加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检测我的 WKWebView 何时完成加载,以便我可以从中获取 URL,例如使用委托方法?

How can I detect when my WKWebView is finished loading so that I can fetch the URL from it such as using the delegate method?

我为 WKWebView 实现了委托方法,但我无法检测到它何时完成加载视频.

I implemented the delegate method for the WKWebView but I can't detect when it is finish loading the video.

import UIKit
import WebKit

class ViewController: UIViewController, WKUIDelegate, WKNavigationDelegate {

    var webView: WKWebView!

    override func viewDidLoad() {
        super.viewDidLoad()
        let preference = WKPreferences()
        preference.javaScriptEnabled = true
        let configuration = WKWebViewConfiguration()
        configuration.preferences = preference
        webView = WKWebView(frame: view.bounds, configuration: configuration)
        view.addSubview(webView)

        webView.uiDelegate = self
        webView.navigationDelegate = self
        webView.load(URLRequest(url: URL(string: "http://www.youtube.com")!))
    }

    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        print("finish loading video")
    }    
}

但是当它完成从 YouTube 加载视频时不会调用上面的方法.

But the method above is not called when it finishes loading the video from YouTube.

非常感谢.

推荐答案

SWIFT 4:

使用 WKNavigationDelegate 的以下委托方法.在哪里可以查看加载完成了??在这里你可以在 webView 中加载 URL.

Use following delegate method of WKNavigationDelegate. Where you can check is finished loading ?? And here you can get URL loaded in webView.

func webView(_ webView: WKWebView, didFinish  navigation: WKNavigation!) 
{
    let url = webView.url?.absoluteString
    print("---Hitted URL--->\(url!)") // here you are getting URL
}

这篇关于检测 WKWebView 何时完成加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆