我有一个视图,它在viewDidLoad()方法内,在执行某些操作后会加载一个子视图:
override func viewDidLoad() {
super.viewDidLoad()
//OTHER STUFF...
let config = WKWebViewConfiguration()
config.userContentController = contentController
self.myWebView = WKWebView(
frame: self.containerView.bounds,
configuration: config
)
self.myWebView.navigationDelegate = self
self.view.addSubview(self.myWebView)
}
加载网络视图后,我需要做一些检查。我该怎么做:
webSubviewDidLoad() {
//do stuff
}
答案 0 :(得分:0)
有一个特定的代表didFinishNavigation
didFinishNavigation documentation
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)
/* add layout constraints that make the webview fitting in your view */
if let url = URL(string: "https://google.com") {
webView.load(URLRequest(url: url))
}
}
}
extension ViewController: WKNavigationDelegate {
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
print("Finished navigating to url \(webView.url)")
}
}
或者您可以检查EstimateProgress属性中的进度
答案 1 :(得分:0)
第1步。添加一个观察者,当您的Web视图加载完成时,如下所示。这段代码应该放在视图控制器类的viewDidLoad方法中。
myWebView.addObserver(self, forKeyPath: #keyPath(WKWebView.isLoading), options: .new, context: nil)
第2步。在视图控制器中实现observeValue(forKeyPath :)方法,执行所需的任何操作,如下所示。
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if keyPath == "isLoading" && !myWebView.isLoading {
// Do your needed action here
}
}
答案 2 :(得分:0)
实现您自己的WebView:
StringWriter writer = new StringWriter();
XMLStreamWriter sw =
XMLOutputFactory.newFactory().createXMLStreamWriter(writer);
StAXResult streamResult = new StAXResult(sw);
System.setProperty("javax.xml.transform.TransformerFactory",
"com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl");
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.ENCODING,document.getXmlEncoding());
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.VERSION, "1.0");
document.setXmlStandalone(false);
DOMSource domSource = new DOMSource(document);
transformer.transform(domSource, streamResult );
System.out.println(writer.toString());