admin管理员组

文章数量:1416080

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.

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.

Share Improve this question asked Apr 26, 2017 at 2:51 LF-DevJourneyLF-DevJourney 28.6k30 gold badges165 silver badges316 bronze badges 2
  • Shouldn’t {a:1, b:2}; be ({a:1, b:2});? – Sebastian Simon Commented Apr 26, 2017 at 2:53
  • {a:1, b:2}; works well here. – LF-DevJourney Commented Apr 26, 2017 at 2:58
Add a ment  | 

1 Answer 1

Reset to default 4

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]
        }
    }

本文标签: iosUnsupported return type when execute javascriptStack Overflow