我使用WKWebview.evaluateJavaScript()来执行javascript,我可以从javascript获取字符串、对象和数组。
evaluateJavaScript("document.getElementById('title').innerHTML;")
/*
output:
Optional(hhhhhhhhhhhhhh)
*/
evaluateJavaScript("[1,2];")
/*
output:
Optional(<__NSArrayM 0x17005faa0>(
1,
2
)
*/
evaluateJavaScript("{a:1, b:2};")
/*
output:
Optional({
a = 1;
b = 2;
})
*/
当我执行这段代码时
evaluateJavaScript("document.getElementById('test').getBoundingClientRect();")
//一个对象 { x: 0, y: 0, width: 0, height: 0, top: 0, right: 0, Bottom: 0, left: 0 }
我收到此错误,
Optional(Error Domain=WKErrorDomain Code=5 "execute JavaScript unsupported return type" UserInfo={NSLocalizedDescription=execute JavaScript unsupported return type}) nil
如有任何帮助,我们将不胜感激,谢谢。
最佳答案
我认为这里 swift 不支持 document.getElementById('liveMovie').getBoundingClientRect();
的结果。
所以我把它改成一个数组,像这样,
self.wk.evaluateJavaScript("var rect = document.getElementById('liveMovie').getBoundingClientRect();[rect.left, rect.top];") {
(result, error) -> Void in
if((result) != nil)
{
self.player?.view?.frame.origin.x = (result as! Array)[0]
self.player?.view?.frame.origin.y = (result as! Array)[1]
}
}
关于javascript - 执行 javascript 时不支持的返回类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43624174/