I’ve been pulling hair all day and I finally figured out how to invoke an Objective-C Block from Xojo.
I’ve seen callbacks passed from functions that can be cast to a delegate and invoked in Xojo, but today I needed to handle a “block”… A handler the WebKit framework hands you to call from webView:decidePolicyForNavigationAction:decisionHandler:
The Apple docs look like this…
- (void)webView:(WKWebView *)webView
I needed to call the “(void (^)(WKNavigationActionPolicy))decisionHandler” part as a delegate.
It didn’t look like something that could be done, as it’s not a “function” and so cannot be used to construct a Xojo delegate.
declare function imp_implementationWithBlock lib "libobjc.dylib" (block as ptr) as ptr
This function takes a “block” as it’s argument, and returns a function. The function can then be used to construct a Xojo delegate and called with .invoke!
I think he was trying to do the opposite of the plugin’s function, instead of creating a block to pass to a function he was trying to take a block passed from a function and invoke it. Not sure the plugin supports that?
However this is the sort of thing Joe does like to tinker with in his “spare” time (such as it is for all of us at the moment)
Would seem a natural addition to the plugin
I’d ping him & see
At the very least he might be able to point out whether there are implications from invoking that obj-C method directly (ownership, retain/ release or lord knows what else) and how it all plays with the Xojo runtime
Joe, I am aware that you are probably in need of a break after conquering the last compiler issues lately. So please don’t take this as an urgent request. But how are the chances for an 64bit-compatible blocks plugin? I’d like to alleviate the MacOSLib problems a bit by publishing Foundation & Core Foundation classes for OS X soon, but without a block option for OS X this would be a very limited selection. Thanks a lot!
(Or Jim: Did you find time to create a similar method to the blocks plugin?)
I keep running into this thread when searching for threads related to HANDLING Block callbacks. Jim, would you please rename this thread to something more specific, such as “Invoking Objective-C blocks”?