Callback from native iOS

125 views
Skip to first unread message

CS

unread,
Jan 1, 2014, 4:06:35 AM1/1/14
to codenameone...@googlegroups.com
Dear all,

The situation is the following. In native code (iOS) I occasionally "get" new data and I would like to "inform/call" (I do not know the correct term) some method in Java part of the project to react to this. I am no iOS coder so some low level, top secret stuff is probably out of my reach. So I am wondering if anybody succeeded in doing so and can share this information. Workaround for Android is no problem, but iOS is another story for me.

To CNO team … do you have a plan for such feature in near future, because if I am not mistaken this is currently not supported.

Regards.

Steve Hannah

unread,
Jan 1, 2014, 12:38:18 PM1/1/14
to codenameone...@googlegroups.com
There is no "official" API for calling back into Java from iOS, but that doesn't mean you can't do it.  CN1 uses XMLVM to translate java to C on iOS so if you following the naming conventions for the conversions of your methods, you can call your converted Java methods directly from C.  Of course if you rely on this stuff, your code will be dependent on their XMLVM back-end so may break if they ever decide to change to a different technology for the iOS back end.  Luckily I wouldn't expect them to be changing that any time soon.

A brief primer of XMLVM naming conventions.

1. Classes are converted to .h and .c pairs with names your_full_class_name.h and your_full_class_name.c  (e.g. java.util.Vector is converted to java_util_Vector.h and java_util_Vector.c).
2. Methods are converted to the form classname_methodname___argtypes(arg1, arg2, etc...). 

An example of a callback can be found in my sockets library.  I created a static method (because static methods are easiest to call) called ca.weblite.codename1.io.Socket.getBuffer(int) which returns a byte array that can be used as the buffer for the socket. 

 So, in order to call this method from iOS, I have to do 2 things.

1. I import the xmlvm.h and ca_weblite_codename1_io_Socket.h header files in my native implementation's header:

2. Call my static method from C:
org_xmlvm_runtime_XMLVMArray* byteArray = ca_weblite_codename1_net_Socket_getBuffer___int(bufferId);


XMLVM provides a few macros for types. E.g. JAVA_OBJECT, JAVA_BOOLEAN, JAVA_INT, etc.. You can see the defines here

A good place to see examples of interacting with XMLVM is in the CodenameOne IOS Port itself. Especially this file:

One difficult thing about developing in iOS for CodenameOne is that you need to send a build to find out if you have an error. If you're doing simple things it might be worth doing a few build cycles and rummaging through the error logs to find your mistakes, but it might be easier to produce a build using "Include source" which will allow you download your project as an Xcode project - then edit your native C implementation as necessary to do what you want - and copy and paste into your Netbeans project source when you have it right.

Steve



--
You received this message because you are subscribed to the Google Groups "CodenameOne Discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to codenameone-discu...@googlegroups.com.
Visit this group at http://groups.google.com/group/codenameone-discussions.
To view this discussion on the web visit https://groups.google.com/d/msgid/codenameone-discussions/83135d34-0530-4330-b1b3-d26bd2a1b48a%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
Steve Hannah
Web Lite Solutions Corp.

Shai Almog

unread,
Jan 1, 2014, 12:54:21 PM1/1/14
to codenameone...@googlegroups.com, computer.s...@gmail.com
We are considering an event dispatch API or callback API for a future revision but don't have concrete plans at the moment. Steve's approach is the way we do it, with a static callback method, its actually pretty simple once you understand the concept.
If you have a Mac you can use the include sources option mentioned: http://www.codenameone.com/how-do-i---use-the-include-sources-feature-to-debug-the-native-code-on-iosandroid-etc.html

computer.s...@gmail.com

unread,
Jan 2, 2014, 12:37:48 PM1/2/14
to codenameone...@googlegroups.com, computer.s...@gmail.com
Dear both,

Thank you both for your really thorough answers … (especially Steve).

I have a Mac and can include sources (I also did this for Android when developing native part, which made things much easier).

So, if Shai says it is pretty easy … than it shall be done without a problem. ;-)


Reply all
Reply to author
Forward
0 new messages