javascript - 在 tvOS 应用程序中从 Swift 调用 JS 函数时出现 "TypeError: undefined is not an object"

标签 javascript ios swift tvos

我在 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

相关的swift代码如下:

//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) {
...

关于javascript - 在 tvOS 应用程序中从 Swift 调用 JS 函数时出现 "TypeError: undefined is not an object",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35050816/

相关文章:

javascript - 我可以获取此代码的演练吗

ios - iOS11字体显示不正确

iphone - 如何在iPhone中下载并显示向下滚动scrollview的服务器图像

ios - 移动按钮从右到左动画 iOS - swift

javascript - jQuery UI 图标问题 - 我如何只显示图像

javascript - Pixi.js 等二维绘图框架如何让 canvas 绘图更快?

javascript - Uncaught ReferenceError : $ is not defined for . js,即使代码看起来一切正常

objective-c - Objective-C - 如何将度分秒转换为 double

ios - 允许 `UIView` 在 mask 到其图层的路径内被点击。如何?

ios - Swift 在 Storyboard中显示 segues(箭头)时以编程方式呈现 ViewController