Objective C callbacks

153 views
Skip to first unread message

Filip Loster

unread,
Jul 28, 2014, 4:48:06 AM7/28/14
to haxe...@googlegroups.com
Hi, I almost finished implementing inapp purchases on iOS - the last thing I'm struggling with is how to call the haxe code from objective C. I need to be able to do that in order to handle the asynchronous callbacks.
I tried passing the function pointers but I have to do something wrong because it crashes each time. 

Skyler Parr

unread,
Aug 8, 2014, 12:29:45 PM8/8/14
to haxe...@googlegroups.com
Hi Filip. IDK if you've figured this out or not. So here's an answer for you.  From haxe you can pass the function pointer to your ExternalInterface.cpp:

    public function callbackTest(onCallback:Void -> Void):Void {
        myextension(onCallback);
    }

    static var myextension= Lib.load("extensions","myextensiontest",1);

Then the method that receives it in your ExternalInterface.cpp will receive the argument and passed into a new instance of AutoGCRoot:

AutoGCRoot * myCallback = 0;

value myextensiontest(value onComplete) {
    myCallback = new AutoGCRoot(onComplete);
    return alloc_null();
}
DEFINE_PRIM(myextensiontest, 1); 

Then when you want to invoke the callback you use the val_call function with the number of arguments you need and invoke the callback with get():

    void notifyTheHaxeCodeImDone() {
        val_call0(myCallback->get());
    }

if you want to pass argument back you call the appropriate val_call:
//1 arg
val_call1(myCallback->get(), someArg);
//2 args
val_call2(myCallback->get(), someArg1, someArg2);
//etc...

Hope this answers your question.

Philippe Elsass

unread,
Aug 8, 2014, 4:20:28 PM8/8/14
to haxe...@googlegroups.com
Nice and simple, Skyler.
 
How to be sure you're calling back Haxe on the right thread?


--
To post to this group haxe...@googlegroups.com
http://groups.google.com/group/haxelang?hl=en
---
You received this message because you are subscribed to the Google Groups "Haxe" group.
For more options, visit https://groups.google.com/d/optout.



--
Philippe

Cristian Baluta

unread,
Aug 9, 2014, 1:50:29 AM8/9/14
to haxe...@googlegroups.com
In objc you can check the thread with this [NSThread isMainThread]
To switch to it you can use dispatch_async(dispatch_get_main_queue(), ^(){ .... });

Filip Loster

unread,
Aug 9, 2014, 3:05:45 PM8/9/14
to haxe...@googlegroups.com
@Skyler Thanks a lot! To be honest I kinda lost faith in getting the response and spend few nights figuring it out on my own. But hopefully your excellent explanation will help people in the future avoid the pleasures of diving trough the tons of obsolete libraries and drinking galons of coffe :)

Philippe Elsass

unread,
Aug 10, 2014, 4:34:36 AM8/10/14
to haxe...@googlegroups.com

For reference, hyperfiction libraries are usually up to date and a good source of inspiration:
https://github.com/hyperfiction

--

Filip Loster

unread,
Aug 10, 2014, 10:55:53 AM8/10/14
to haxe...@googlegroups.com
I hoped they would, but it seems that inthebox-macros is no longer supported (or at least I wasn't able to run it using latest haxe and openfl) and hyperfiction classes make heavy use of that.


On Monday, July 28, 2014 10:48:06 AM UTC+2, Filip Loster wrote:

Achmad Aulia Noorhakim

unread,
Aug 10, 2014, 12:04:57 PM8/10/14
to haxe...@googlegroups.com

I use the OpenFL's iap extension, for my goto reference for doing iOS extension.

--

shoe[box]

unread,
Aug 11, 2014, 3:39:26 AM8/11/14
to haxe...@googlegroups.com
Hi,

What are your issues with inthebox-macros ?
I'm using it everyday and it work perfectly on the CPP/IOS side.
And there is a little know bug on the JNI side (about non-static calls)

Please report any issue you have here:
https://github.com/shoebox/inthebox-macros


Thanks

Filip Loster

unread,
Aug 11, 2014, 5:19:37 AM8/11/14
to haxe...@googlegroups.com
Thanks for this post, I will check it out once again and submit any issue I had with them. 
Cant remember right now, but they didn't work out of the box and that made me to belive they didn't work at all and stopped investigating.
Reply all
Reply to author
Forward
0 new messages