Unsupported return type when execute javascript

2019-05-29 14:20发布

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;
})
*/

While I execute this code

evaluateJavaScript("document.getElementById('test').getBoundingClientRect();") //an object of { x: 0, y: 0, width: 0, height: 0, top: 0, right: 0, bottom: 0, left: 0 }

I get this error,

Optional(Error Domain=WKErrorDomain Code=5 "execute JavaScript unsupported return type" UserInfo={NSLocalizedDescription=execute JavaScript unsupported return type}) nil

Any help will appreciated, thank you.

1条回答
在下西门庆
2楼-- · 2019-05-29 14:47

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]
        }
    }
查看更多
登录 后发表回答