在 Xcode 中使用 Swift 确定在 webView 中完成加载网站

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28149097/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 06:32:12  来源:igfitidea点击:

Determinate finish loading website in webView with Swift in Xcode

iosxcodeswiftwebviewloading

提问by Mirko Brombin

i'm trying to create a webapp with swift in xcode, this is my current code:

我正在尝试在 xcode 中使用 swift 创建一个 webapp,这是我当前的代码:

IBOutlet var webView: UIWebView!

    var theBool: Bool = false
    var myTimer = NSTimer()
    @IBOutlet var progressBar: UIProgressView!

override func viewDidLoad() {
   super.viewDidLoad()
   let url = NSURL(string: "http://stackoverflow.com")
   let request = NSURLRequest(URL: url)
   webView.loadRequest(request)
}

I've a question, How i can determinate the finish loading of the page in webView?

我有一个问题,如何确定 webView 中页面的完成加载?

Do you know a documentation that contains all the definitions of the WebView? I mean .. (start / end load, the current url, title page, etc ..)?

您知道包含 WebView 的所有定义的文档吗?我的意思是..(开始/结束加载,当前网址,标题页等..)?

(Sorry for my English).

(对不起我的英语不好)。

回答by Marius Fanu

Through the UIWebViewdelegate call.

通过UIWebView委托调用。

You need to set your webViews delegateto the current controller, and conform to the UIWebViewDelegateprotocol. When the webView finished loading the page func webViewDidFinishLoad(_ webView: UIWebView)will get called.

您需要将您的 webViewsdelegate设置为当前控制器,并符合UIWebViewDelegate协议。当 webView 完成加载时,页面func webViewDidFinishLoad(_ webView: UIWebView)将被调用。

回答by Kris Roofe

For WKWebview there is also wknavigationdelegate didfinish, but not do the trick as the SO question WKWebView didn't finish loading, when didFinishNavigation is called - Bug in WKWebView?and this answershow.

对于 WKWebview 也有wknavigationdelegate didfinish,但不要这样做,因为问题WKWebView 没有完成加载,当 didFinishNavigation 被调用时 - WKWebView 中的错误?这个答案显示。

I also found when the page to load is very complicate and dinamic, UIWebview's webViewDidFinishLoad(- webView: UIWebView)also not works.

我还发现当要加载的页面非常复杂和动态时,UIWebviewwebViewDidFinishLoad(- webView: UIWebView)也不起作用。

And I think use native swift or objective-c to detect when the page is loaded is a bad choice. The more flexable and effective way is to use javascript to call the native swift code when page finishing loading. What's more this also work for a specific dom finishing loading event and very dynamic content.

而且我认为使用原生 swift 或 Objective-c 来检测页面何时加载是一个糟糕的选择。更灵活有效的方式是在页面加载完成时使用javascript调用原生的swift代码。更重要的是,这也适用于特定的 dom 完成加载事件和非常动态的内容。

For how to call swift from javascript refer to this post.

有关如何从 javascript 调用 swift 的信息,请参阅这篇文章

Here is a sample code for dynamic content with anjularjs.

这是使用 anjularjs 的动态内容示例代码。

js,

js,

var homeApp = angular.module('home', ['ui.bootstrap', 'ngAnimate']);
homeApp
    .directive("printerinfo",
    function() {
        return {
            restrict: "E",
            link: function() {       //call navite swift when page finishing loading
                try {
                    window.webkit.messageHandlers.testHandler.postMessage("Hello from JavaScript");
                } catch(err) {
                    console.log('The native context does not exist yet');
                }
            },

            templateUrl: "/static/tpls/cloud/app/printerinfo.php",
        }
    })

swift3,

迅捷3,

func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
    print(message.name)
    if(message.name == "testHandler") {
        //webpage content loaded
        print(Date())
        print("javascript test call swift")
    }

Here I use angularjs to check element ready, you can also refter how-to-call-a-function-after-a-div-is-readyor check-if-a-given-dom-element-is-readyor jquery-ready-equivalent-event-listener-on-elementsfor more.

这里我使用 angularjs 来检查元素就绪,你也可以参考how-to-call-a-function-after-a-div-is-readycheck-if-a-given-dom-element-is-readyjquery -ready-equivalent-event-listener-on-elements了解更多。