我在陷入困境的tvOS应用中从Swift调用JS函数时遇到错误。
我已经设置了可以从JS调用Swift的功能,但是可以正常工作,但是当尝试从Swift调用JS函数时,我在Safari调试器中收到以下错误:
TypeError: undefined is not an object
(anonymous function)
JSValueToObject
-[JSValue callWithArguments:]
_TFFC13tvOSShortGame11AppDelegate17callPlayVideoInJSFS0_FSST_U_FCSo9JSContextT_
_TTRXFo_oCSo9JSContext_dT__XFdCb_dS__dT__
-[IKAppContext _doEvaluate:]
-[IKAppContext _evaluate:]
__41-[IKAppContext evaluate:completionBlock:]_block_invoke
-[IKAppContext _sourcePerform]
IKRunLoopSourcePerformCallBack
__CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__
__CFRunLoopDoSources0
__CFRunLoopRun
CFRunLoopRunSpecific
CFRunLoopRun
-[IKAppContext _jsThreadMain]
__NSThread__start__
_pthread_body
_pthread_body
thread_start
相关的快速代码如下:
//From JS to Swift
//call this method once after setting up your appController.
func createGetVimeoURL(){
//allows us to access the javascript context
appController?.evaluateInJavaScriptContext({(evaluation: JSContext) -> Void in
//this is the block that will be called when javascript calls getVimeoURL(str)
let getVimeoURL : @convention(block) (String) -> Void = {
(videoName : String) -> Void in
//5. return the vimeo url to the JS code for the passed in videoName
self.getTempVimeoURL(videoName)
}
//this creates a function in the javascript context called "getVimeoURL".
//calling getVimeoURL(str) in javascript will call the block we created above.
evaluation.setObject(unsafeBitCast(getVimeoURL, AnyObject.self), forKeyedSubscript: "getVimeoURL")
}, completion: {(Bool) -> Void in
//evaluation block finished running
})
}
//From Swift to JS
//when callPlayVideoInJS() is called, playVideo(url) will be called in JavaScript.
func callPlayVideoInJS(url : String){
//allows us to access the javascript context
appController!.evaluateInJavaScriptContext({(evaluation: JSContext) -> Void in
//get a handle on the "playVideo" method that you've implemented in JavaScript
let playVideo = evaluation.objectForKeyedSubscript("playVideo")
//Call your JavaScript method with an array of arguments
playVideo.callWithArguments([url])
}, completion: {(Bool) -> Void in
//evaluation block finished running
})
}
//4. getTempVimeoURL from videoName
func getTempVimeoURL (videoName : String) -> String {
return "http://techslides.com/demos/sample-videos/small.mp4"
}
以及应该由swift(我手动创建)调用的javascript函数:
playVideo: function (url) {
if(url) {
//2
var player = new Player();
var playlist = new Playlist();
var mediaItem = new MediaItem("video", url);
player.playlist = playlist;
player.playlist.push(mediaItem);
player.present();
}
}
当我在appController didFinishLaunchingWithOptions中调用createGetVimeoURL时,会创建getVimeoURL javascript函数。
但是我无法弄清楚为什么我在调用callPlayVideoInJS时收到javascript错误,但这可能很简单!
任何帮助表示赞赏。
看来您没有使用objectForKeyedSubscript访问适当的JS方法
改变
let playVideo = evaluation.objectForKeyedSubscript("vimeoVideoURL")
到
let playVideo = evaluation.objectForKeyedSubscript("playVideo")
[更新]您还需要确保可以从适当的上下文和范围访问playVideo方法。尝试将playVideo方法放入application.js中:
var playVideo = function (url) {
...
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句