ObjC callback timing

@jfperusse Ah, that would explain it then :smile:
Very nice work in general though!
Thanks!

Hi @Steppers! Should be working with latest beta version. Here’s a simplified version I’ve been using:

Handler = objc.delegate("WKScriptMessageHandler")

function Handler:userContentController_didReceiveScriptMessage_(
    objUserContentController, objMessage)
    print(objMessage.body)
end

function setup()
    local controller = objc.cls.WKUserContentController()
    local logHandler = Handler()
    controller:addScriptMessageHandler_name_(logHandler, "log")
 
    local config = objc.cls.WKWebViewConfiguration()
    config.userContentController = controller
    
    local wk = objc.cls.WKWebView:alloc()
    wk:initWithFrame_configuration_(objc.viewer.view.bounds, config)
    wk:loadHTMLString_baseURL_([[
        <script>
        window.webkit.messageHandlers.log.postMessage("Hello World!");
        </script>
        <body><center>Hello World!</center></body>
    ]], objc.cls.NSURL:URLWithString_(""))

    objc.viewer.view:insertSubview_atIndex_(wk, 1)
    objc.viewer.view.subviews[1]:removeFromSuperview()
end

@jfperusse Amazing, I’ve just checked it out and it works perfectly! :smiley:

Thanks a bunch!
o7

@Steppers Awesome, thank you so much!

@jfperusse What is the best link to figure out the objc stuff. I was looking at this link

https://developer.apple.com/documentation/technologies

but I’m not sure if that’s the best for someone that knows nothing about objc.

@dave1707 I’d say the official Apple documents are generally not too bad (switching them to Objective-C). However, they’re often not enough to tell the exact method signatures and I have to look for online examples. I also personally rely a lot on Xcode. I create an empty project, and then I can more easily look at the frameworks header files to find what I am looking for.

@jfperusse Any idea why this objc.delegate usage doesn’t work?

function setup()
    objc.delegate("WKScriptMessageHandler") -- Works
    objc.delegate("WKScriptMessageHandlerWithReply") -- Fails (warning in log)
end

@Steppers What’s your iOS version?

@jfperusse 15.2.1

@Steppers Thanks, there must be something we need to do on our side to get it included. I’ll look into it!

Hi @Steppers, turns out some protocols get removed from Codea at compile time when they are not referenced, but not all of them.

The best fix we found is to manually reference missing protocols when we encounter them, so I’ve added a reference to WKScriptMessageHandlerWithReply which should make it in a future beta version.

Please let us know if you encounter other missing protocols.

Thank you!

@Steppers you reference a zip file in the original post, is that still available somewhere?

I’m sorry if this is necroposting, I’m trying to learn more about the objc bindings and there seem to only be a handful of examples here in the forums.

@UberGoober Hey, sorry I’ve had a look but can’t seem to find it.

The JavaScript project on WebRepo should be a pretty good example though.