问题描述
我使用 WKWebview.evaluateJavaScript() 来执行 javascript,我可以从 javascript 中获取字符串、对象和数组.
I use WKWebview.evaluateJavaScript() to execute javascript, I can get string, object and array from 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 "执行不支持的 JavaScript 返回类型"UserInfo={NSLocalizedDescription=执行 JavaScript 不支持的返回类型}) nil
任何帮助将不胜感激,谢谢.
Any help will appreciated, thank you.
推荐答案
我认为这里 document.getElementById('liveMovie').getBoundingClientRect();
的结果是 swift 不支持的.
I think here the result of document.getElementById('liveMovie').getBoundingClientRect();
is not support by swift.
所以我把它改成一个数组,就像这样,
So I change it to an array, like this,
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时不支持的返回类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!