永远不会调用WKWebView内容加载函数

编程入门 行业动态 更新时间:2024-10-20 20:33:18
本文介绍了永远不会调用WKWebView内容加载函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我尝试在WKWebView中的内容完全加载后调用一个函数。我在Apple Swift WKNavigation文档中找到了didFinishNavigation函数。

i try to get a function called after my Content inside WKWebView is fully loaded. I found the "didFinishNavigation" function at the Apple Swift WKNavigation Documentation.

func webView(webView: WKWebView!, didFinishNavigation navigation: WKNavigation!) { println("WebView content loaded.") }

但是函数永远不会被调用。

But the function never get called.

import UIKit import WebKit class ViewController: UIViewController WKNavigationDelegate { override func loadView() { super.loadView() self.webView = WKWebView(frame:self.containerView.frame, configuration: WKWebViewConfiguration()) self.containerView.addSubview(webView!) self.containerView.clipsToBounds = true } override func viewDidLoad() { super.viewDidLoad() var url = NSURL(string:"google/") var req = NSURLRequest(URL:url) self.webView!.loadRequest(req) } func webView(webView: WKWebView!, didFinishNavigation navigation: WKNavigation!) { println("WebView content loaded.") } }

推荐答案

你没有设置navigationDelegate。设置它应该没问题。

You are not setting the navigationDelegate. Set it and it should be fine.

class ViewController: UIViewController, WKNavigationDelegate { override func viewDidLoad() { super.viewDidLoad() let noLayoutFormatOptions = NSLayoutFormatOptions(rawValue: 0) let webView = WKWebView(frame: CGRectZero, configuration: WKWebViewConfiguration()) webView.setTranslatesAutoresizingMaskIntoConstraints(false) webView.navigationDelegate = self view.addSubview(webView) view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[webView]|", options: noLayoutFormatOptions, metrics: nil, views: ["webView": webView])) view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[webView]|", options: noLayoutFormatOptions, metrics: nil, views: ["webView": webView])) let url = NSURL(string: "google") let request = NSURLRequest(URL: url) webView.loadRequest(request) } func webView(webView: WKWebView!, didFinishNavigation navigation: WKNavigation!) { print("Finished navigating to url \(webView.url)"); } }

这是一个更好的版本使用Swift 3.

And here is a bit better version with Swift 3.

class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() let configuration = WKWebViewConfiguration() let webView = WKWebView(frame: .zero, configuration: configuration) webView.translatesAutoresizingMaskIntoConstraints = false webView.navigationDelegate = self view.addSubview(webView) [webView.topAnchor.constraint(equalTo: view.topAnchor), webView.bottomAnchor.constraint(equalTo: view.bottomAnchor), webView.leftAnchor.constraint(equalTo: view.leftAnchor), webView.rightAnchor.constraint(equalTo: view.rightAnchor)].forEach { anchor in anchor.isActive = true } if let url = URL(string: "google/search?q=westworld") { webView.load(URLRequest(url: url)) } } } extension ViewController: WKNavigationDelegate { func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) { print("Finished navigating to url \(webView.url)") } }

更多推荐

永远不会调用WKWebView内容加载函数

本文发布于:2023-11-26 20:56:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1635162.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:永远不会   函数   加载   内容   WKWebView

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!