执行javascript时不支持的返回类型 [英] Unsupported return type when execute javascript

查看:156
本文介绍了执行javascript时不支持的返回类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 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

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.

推荐答案

我认为这里 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时不支持的返回类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆