2017-04-26 91 views
0

在我的ios应用程序中,我使用WKWebview.evaluateJavaScript()来执行javascript以获取div boundingRect。在我的网页上,我用anjularjs渲染我的网页的一部分,该网页的内容是,swift如何在anjular渲染页面后执行javascript?

<body ng-app="home" ng-controller="HomeController" style="min-height:700px;"> 
    <printerinfo></printerinfo> 
    </body> 
    <div id="movie" style="border: 1px grey dotted; width: 120px; height: 90px;"></div> 

迅速

func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) { 
    self.wk.evaluateJavaScript("var rect = document.getElementById('movie').getBoundingClientRect(); [rect.x, rect.y, rect.width, rect.height, rect.top, rect.right, rect.bottom, rect.left];") { (result, error) -> Void in 
     print(result) 
    } 
} 

从结果,

Optional(<__NSArrayM 0x170251010>(
<null>, 
<null>, 
120, 
90, 
0, 
120, 
90, 
0 
) 
) 

的boundingRect是视口的起源,看起来身体在视口中没有空间。我认为这是因为当网页加载时,angualrjs没有呈现身体部分。

那么如何在anjularjs使用swift3渲染页面后执行javascript?

非常感谢。

+0

查看本帖回复http://stackoverflow.com/a/43672099/6521116 http://stackoverflow.com/q/28149097/6521116 –

回答

0

我认为使用native swift或objective-c来检测页面何时加载是一个不好的选择。更灵活有效的方法是在页面加载完成时使用javascript调用本地swift代码。更重要的是,这也适用于特定的dom整理加载事件和非常动态的内容。

欲了解详情请致电post